gpt4 book ai didi

python - pyvirtualdisplay 在 Xvfb 上的工作剂量是多少,或者 pyvirtualdisplay 可以打开多少个 Xvfb?

转载 作者:太空宇宙 更新时间:2023-11-03 14:37:39 26 4
gpt4 key购买 nike

  1. 我使用以下代码打开 saveral Xvfb 进行测试,但遇到了一些问题:
    A. Xvfb 的状态(代码后面)有的进程是 Z 或 SL,有的进程是 Z+ 或 SL+,但 python 进程正常进行剂量是什么意思?
    B. 请求时代码运行正常,但最后出现异常(跟随状态)。

代码

#!/usr/bin/env python3
# encoding: utf-8
import os
import time
import random
import multiprocessing
from pyvirtualdisplay import Display
from selenium import webdriver

def main():
display = Display(visible=0, size=(800, 600))
display.start()
print(os.getpid())
browser = webdriver.Firefox()
#time.sleep(20)
for j in range(30):
browser.get('http://www.google.com')
print(browser.title)
time.sleep(1) # !!!!!! this is sleep time
browser.quit()
display.stop()

if __name__ == '__main__':
tl = []
for i in range(10):
tl.append(multiprocessing.Process(target=main))
start = time.time()
for j in tl:
j.start()
for j in tl:
j.join()
print("end {}".format((time.time() - start)))

状态

root     18503  0.0  0.0      0     0 ?        Z    13:40   0:00 [Xvfb] <defunct>
root 18504 0.1 0.7 233684 57364 ? Sl 13:40 0:22 Xvfb -br -nolisten tcp -screen 0 800x800x24 :1049
root 18506 0.0 0.0 0 0 ? Z 13:40 0:00 [Xvfb] <defunct>
root 18508 0.0 0.0 0 0 ? Z 13:40 0:00 [Xvfb] <defunct>
root 18509 0.0 0.0 0 0 ? Z 13:40 0:00 [Xvfb] <defunct>
root 18514 0.0 0.5 221140 44736 ? Sl 13:40 0:11 Xvfb -br -nolisten tcp -screen 0 800x800x24 :1052
root 18515 0.0 0.0 0 0 ? Z 13:40 0:00 [Xvfb] <defunct>
root 18516 0.0 0.0 0 0 ? Z 13:40 0:00 [Xvfb] <defunct>
root 18517 0.0 0.0 0 0 ? Z 13:40 0:00 [Xvfb] <defunct>
root 18520 0.0 0.4 212832 36468 ? Sl 13:40 0:04 Xvfb -br -nolisten tcp -screen 0 800x800x24 :1055

异常

Process Process-10:
Traceback (most recent call last):
File "/usr/lib/python3.5/multiprocessing/process.py", line 249, in _bootstrap
self.run()
File "/usr/lib/python3.5/multiprocessing/process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "/root/PycharmProjects/MyShiYanLou/auto_/test_xvfb.py", line 27, in main
browser.quit()
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 183, in quit
RemoteWebDriver.quit(self)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 592, in quit
self.execute(Command.QUIT)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 297, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Failed to decode response from marionette

最佳答案

您遇到的异常不是由于 pyvirtualdisplay 可以打开一定数量的 Xvfb 而引起的,而是由于 selenium 将在您打开的最后一个虚拟显示器中打开。

想象一下有两个进程AB的情况

  1. A 打开 Xvfb
  2. B 打开 Xvfb
  3. AB(最后一个打开的)打开的虚拟显示器中打开 Webdriver
  4. B关闭 Xvfb
  5. A 尝试获取网页,但由于打开 webdriver 的 Xvfb 已关闭而失败

避免该错误的最佳方法是在运行多个进程之前打开一个 Xvfb:

代码

#!/usr/bin/env python3
# encoding: utf-8
import os
import time
import multiprocessing
from pyvirtualdisplay import Display
from selenium import webdriver

def main():
print(os.getpid())
browser = webdriver.Firefox()
for j in range(30):
browser.get('http://www.google.com')
print(browser.title)
time.sleep(1) # !!!!!! this is sleep time
browser.quit()


if __name__ == '__main__':
display = Display(visible=0, size=(800, 600))
display.start()

tl = []
for i in range(10):
tl.append(multiprocessing.Process(target=main))
start = time.time()
for j in tl:
j.start()
for j in tl:
j.join()

display.stop()

print("end {}".format((time.time() - start)))

关于python - pyvirtualdisplay 在 Xvfb 上的工作剂量是多少,或者 pyvirtualdisplay 可以打开多少个 Xvfb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46807348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com