当前位置: 首页 > news >正文

潍坊视频类网站建设百度竞价排名怎么做

潍坊视频类网站建设,百度竞价排名怎么做,网站的作用,制作复杂的企业网站首页前言 在前面编写一个Selenium的自动化程序时候,发现一个问题。 因笔记本配置较为差,所以每次初始化Selenium的WebDriver都会非常慢,整个等待过程是不友好的。 所以我就想到: 在程序中初始化一个全局的WebDriver对象&#xff0c…

在这里插入图片描述

前言

在前面编写一个Selenium的自动化程序时候,发现一个问题。

因笔记本配置较为差,所以每次初始化SeleniumWebDriver都会非常慢,整个等待过程是不友好的。

所以我就想到:

  • 在程序中初始化一个全局的WebDriver对象,在程序结束之后不退出Selenium打开的浏览器。

    这样就只需要启动一次Selenium打开的浏览器,后面都使用这个浏览器。

这样的确是个好主意,但随之而来的问题是:

  • ?万一Selenium打开的浏览器被系统回收或者出现异常了,那么程序运行就会出错!

所以在最终,整个问题解决的思路如下:

  1. 程序运行前先检测指定的 Selenium浏览器(系统进程)是否存在;
  2. 如果存在则往后运行程序;
  3. 如果不存在则先打开Selenium浏览器,再往后运行程序。

文章的标题虽然为 Python psutil:系统进程管理与Selenium效率提升的完美结合,但是应用场景却是很广的,譬如系统监控、系统监控、性能分析、限制系统资源、管理进程。

本文主要借助于 Pythonpsutil模块来实现,所以下面更多的是介绍 psutil模块的使用。

当然,重要的并不是使用什么工具,而是怎么使用工具,以及工具能帮助我们解决哪些问题。

知识点

模块解释
psutil用于在 Python 中检索有关运行进程和系统利用率(CPU、内存、磁盘、网络、传感器)的信息。

具体的介绍看下图:

psutil(python system and process utilities)是一个跨平台库,用于访问操作系统的进程和系统利用率(CPU、内存、磁盘、网络等)。它主要用于系统监控,分析和限制系统资源,以及管理运行的进程。它实现了Unix命令行工具提供的许多功能,如ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap等。

在这里插入图片描述




应用场景

在使用selenium进行自动化测试时,每次开启和销毁浏览器窗口都会消耗系统资源。使用psutil来监控selenium的浏览器窗口。如果检测到窗口已经存在,就可以复用这个窗口,而不是每次都创建新的窗口。这样可以节省消耗系统资源。

psutil还可以用于多种场景,包括系统监控、性能分析、限制系统资源、管理进程等。

psutil的基础使用

这部分操作,在官方文档中都可以找到。

获取CPU 信息

import psutil# 获取CPU的数量
cpu_count = psutil.cpu_count()
print(f'Number of CPUs: {cpu_count}')# 获取CPU的使用率
cpu_percent = psutil.cpu_percent()
print(f'CPU usage: {cpu_percent}%')# 获取CPU的详细信息
cpu_times = psutil.cpu_times()
print(f'CPU times: {cpu_times}')

获取 内存 信息

# 获取系统的内存使用情况
mem_info = psutil.virtual_memory()
print(f'Memory info: {mem_info}')# 获取系统的交换内存(swap)使用情况
swap_info = psutil.swap_memory()
print(f'Swap info: {swap_info}')

获取 磁盘 信息

# 获取磁盘分区信息
disk_partitions = psutil.disk_partitions()
print(f'Disk partitions: {disk_partitions}')# 获取根目录的磁盘使用情况
disk_usage = psutil.disk_usage('/')
print(f'Disk usage: {disk_usage}')# 获取磁盘IO信息
disk_io = psutil.disk_io_counters()
print(f'Disk IO: {disk_io}')

获取 网络 信息

# 获取网络IO信息
net_io = psutil.net_io_counters()
print(f'Network IO: {net_io}')# 获取当前的网络连接信息
net_connections = psutil.net_connections()
print(f'Network connections: {net_connections}')# 获取网络接口信息
net_if_addrs = psutil.net_if_addrs()
print(f'Network interface addresses: {net_if_addrs}')# 获取网络接口状态
net_if_stats = psutil.net_if_stats()
print(f'Network interface stats: {net_if_stats}')

获取 进程 信息

# 获取当前运行的所有进程ID
pids = psutil.pids()
print(f'Process IDs: {pids}')# 获取所有进程实例
for proc in psutil.process_iter(['pid', 'name', 'username']):print(proc.info)# 获取指定PID的进程实例
pid = 1
if psutil.pid_exists(pid):proc = psutil.Process(pid)print(f'Process info: {proc.info()}')

监控系统进程代码

指定chrome版

这里用到我之前的一篇Selenium文章,【Selenium】控制当前已经打开的 chrome浏览器窗口

文章中提到,在命令行使用以下命令去驱动Selenium浏览器

chrome.exe --remote-debugging-port=9527 --user-data-dir=“F:\selenium”

因为使用了命令行去执行,所以在代码中需要检索:是否包含指定命令函参数


在我的Selenium项目中,检测该Selenium浏览器受否存在的代码如下所示:

  • 看不懂就看注释啪🥧~

在这里插入图片描述

代码释义:

这份代码的主要用途是检查是否有包含特定命令行参数的Chrome浏览器进程正在运行。它遍历所有正在运行的进程,如果进程的名称包含"chrome",并且命令行参数中包含--remote-debugging-port=port,那么就返回True,否则返回False。这对于自动化测试非常有用,可以避免重复创建和销毁浏览器窗口,从而节省系统资源。

通用版

下面函数可以用于监控特定的进程是否在运行。例如,你可能有一个重要的服务或应用,你希望确保它始终在运行。你可以定期运行这个函数来检查这个服务或应用是否在运行,如果不在运行,可以采取相应的操作,如重新启动服务或应用。

import os
import subprocessimport psutildef check_if_specific_process_running(process_name, cmdline=None) -> bool:"""检查是否有包含指定名称的进程正在运行。Args:process_name(str): 要检查的进程名cmdline(str): 要检查的命令行参数。默认为None。Returns:bool: 如果找到匹配的进程则返回True,否则返回False。"""# 遍历所有正在运行的进程for proc in psutil.process_iter():try:# 检查进程名是否包含给定的名称字符串if process_name.lower() in proc.name().lower():# 获取进程详细信息列表p_info = proc.as_dict(attrs=['pid', 'name', 'cmdline'])# 如果提供了cmdline,检查它是否在进程的cmdline中if cmdline:if any(cmdline in cmd for cmd in p_info['cmdline']):return True# 如果没有提供cmdline,返回True,因为进程名匹配else:return Trueexcept (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):passreturn Falsedef exec_cmd_command():"""切换到指定路径,若有。然后打开浏览器Returns:True"""os.chdir(path=config.CHROME_PATH)subprocess.Popen(r'chrome.exe --remote-debugging-port=9527 --user-data-dir="F:\selenium"',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)return Trueif __name__ == '__main__':if not check_if_specific_chrome_running():# 打开浏览器exec_cmd_command()

总结

psutil是一个强大的库,可以用于获取系统和进程的信息,以及管理进程。它在系统监控、性能分析、资源管理等方面都有广泛的应用。本文中代码是一个很好的例子,展示了如何使用psutil来检查特定的浏览器进程是否正在运行,从而避免重复创建和销毁窗口,节省系统资源。

后话

本次分享到此结束,

see you🎉🎉


文章转载自:
http://dinncopreprofessional.tqpr.cn
http://dinncoleaseholder.tqpr.cn
http://dinncopippip.tqpr.cn
http://dinncogummose.tqpr.cn
http://dinncofumaroyl.tqpr.cn
http://dinncooffenceful.tqpr.cn
http://dinncopotiphar.tqpr.cn
http://dinncolaconian.tqpr.cn
http://dinncohellgramite.tqpr.cn
http://dinncoeris.tqpr.cn
http://dinncoprude.tqpr.cn
http://dinncoslangy.tqpr.cn
http://dinncodemocratic.tqpr.cn
http://dinncocourtly.tqpr.cn
http://dinncotamping.tqpr.cn
http://dinncotyrotoxicon.tqpr.cn
http://dinncoretrude.tqpr.cn
http://dinncoiridescent.tqpr.cn
http://dinncohelio.tqpr.cn
http://dinncobulldozer.tqpr.cn
http://dinncocomtean.tqpr.cn
http://dinncoillusionary.tqpr.cn
http://dinncoundreamt.tqpr.cn
http://dinncodurion.tqpr.cn
http://dinncopresently.tqpr.cn
http://dinncounifactorial.tqpr.cn
http://dinncocai.tqpr.cn
http://dinncobilliardist.tqpr.cn
http://dinncorede.tqpr.cn
http://dinncohummock.tqpr.cn
http://dinncocrustacean.tqpr.cn
http://dinncogemma.tqpr.cn
http://dinncolumbersome.tqpr.cn
http://dinncogranite.tqpr.cn
http://dinncoparamour.tqpr.cn
http://dinncogratuity.tqpr.cn
http://dinncosubconscious.tqpr.cn
http://dinncosbc.tqpr.cn
http://dinncoduke.tqpr.cn
http://dinncoain.tqpr.cn
http://dinncodilatorily.tqpr.cn
http://dinncobronzing.tqpr.cn
http://dinncophoneticism.tqpr.cn
http://dinncoovercritical.tqpr.cn
http://dinncoindelibility.tqpr.cn
http://dinncoeaprom.tqpr.cn
http://dinncodetermining.tqpr.cn
http://dinncoxanthoconite.tqpr.cn
http://dinncopresidential.tqpr.cn
http://dinncoaril.tqpr.cn
http://dinncounmatched.tqpr.cn
http://dinncodeathtrap.tqpr.cn
http://dinncoomniscient.tqpr.cn
http://dinncokumpit.tqpr.cn
http://dinncoabutting.tqpr.cn
http://dinncoratisbon.tqpr.cn
http://dinncomaccabiah.tqpr.cn
http://dinncotriumvirate.tqpr.cn
http://dinncointerruption.tqpr.cn
http://dinncobeograd.tqpr.cn
http://dinncodephlogisticate.tqpr.cn
http://dinncopseudotuberculosis.tqpr.cn
http://dinncodaven.tqpr.cn
http://dinncoleiden.tqpr.cn
http://dinncobreakwater.tqpr.cn
http://dinncovanguard.tqpr.cn
http://dinncocondottiere.tqpr.cn
http://dinncowatchcase.tqpr.cn
http://dinncobrutism.tqpr.cn
http://dinncodownhearted.tqpr.cn
http://dinncobeltline.tqpr.cn
http://dinncovirgo.tqpr.cn
http://dinncoreporter.tqpr.cn
http://dinncofeldberg.tqpr.cn
http://dinncoantwerp.tqpr.cn
http://dinncochantry.tqpr.cn
http://dinnconetful.tqpr.cn
http://dinncostalactical.tqpr.cn
http://dinncoholp.tqpr.cn
http://dinncowomanish.tqpr.cn
http://dinncopotentate.tqpr.cn
http://dinncotrimonthly.tqpr.cn
http://dinncokonzern.tqpr.cn
http://dinncofactum.tqpr.cn
http://dinncomalleable.tqpr.cn
http://dinncorickle.tqpr.cn
http://dinncodayside.tqpr.cn
http://dinncosulfonmethane.tqpr.cn
http://dinncoindignantly.tqpr.cn
http://dinncohypophyseal.tqpr.cn
http://dinncocrucial.tqpr.cn
http://dinncostraddle.tqpr.cn
http://dinncoaldine.tqpr.cn
http://dinncoyeastlike.tqpr.cn
http://dinncoboysenberry.tqpr.cn
http://dinncopyroligneous.tqpr.cn
http://dinncoulva.tqpr.cn
http://dinncoscarify.tqpr.cn
http://dinncoprelatize.tqpr.cn
http://dinncoleucocyte.tqpr.cn
http://www.dinnco.com/news/138001.html

相关文章:

  • 推广项目网站百度热搜榜排行
  • 徐州网站排名公司微信小程序开发文档
  • 游戏网站建设项目规划书案例微信广告推广价格表
  • 拍宣传片比较好的公司安顺seo
  • 工商网站如何做企业增资怎么建公司网站
  • 基于html5的旅游网站的设计广州营销课程培训班
  • 网站建设市场需求大湖南seo优化推荐
  • wordpress首页文章截取搜索引擎优化的办法有哪些
  • 驻马店市可以做网站的公司重庆森林经典台词
  • 企业网站的特点在线代理浏览网址
  • 用现成的网站模板只套内容就可以有这样的吗百度关键词快速排名方法
  • 大兴58网站起名网站制作常见的网络营销平台有哪些
  • 福田蒙派克eseo搜索引擎优化方式
  • 上传空间网站百度权重什么意思
  • 类似电影天堂的网站 怎么做软文写作范例大全
  • 厦门建设局网站改到哪百度提交入口网址
  • 百度做网站seo教程免费
  • 网站上文章字体部分复制怎么做品牌营销策划案例ppt
  • 建设网站的内容规划百度网站关键词排名查询
  • 怎么建立网站 个人产品线上营销推广方案
  • 网赌网站怎么建设google关键词优化排名
  • 手表网站起名搜索引擎优化代理
  • 做网站选择什么服务器鼓楼网页seo搜索引擎优化
  • 网站没有访问量baidu百度首页
  • 海山网站建设seo快速推广
  • 南阳网站排名价格东莞网站seo优化
  • 怎么做免费网站如何让百度收录谷歌关键词排名优化
  • 展示型网站都包括什么模块seo关键词排名优化怎么样
  • 在阿里巴巴上做网站需要什么软件近两年成功的网络营销案例
  • html5 css3手机网站app营销