Vla*_*gas 7 python selenium python-multiprocessing
我有3个驱动程序(Firefox浏览器),我希望它们do something在网站列表中.
我有一个工人定义为:
def worker(browser, queue):
while True:
id_ = queue.get(True)
obj = ReviewID(id_)
obj.search(browser)
if obj.exists(browser):
print(obj.get_url(browser))
else:
print("Nothing")
Run Code Online (Sandbox Code Playgroud)
因此,worker只会访问包含id的队列并使用浏览器执行某些操作.
我想拥有一个工作池,这样一旦工人完成了使用浏览器在id_定义的网站上做某事,那么它会立即开始使用相同的浏览器在队列中找到的下一个id_上做某事.我有这个:
pool = Pool(processes=3) # I want to have 3 drivers
manager = Manager()
queue = manager.Queue()
# Define here my workers in the pool
for id_ in ids:
queue.put(id_)
for i in range(3):
queue.put(None)
Run Code Online (Sandbox Code Playgroud)
我有一个问题,我不知道如何定义我的工人,以便他们在池中.对于每个驱动程序,我需要分配一个worker,并且所有worker都共享同一个id队列.这可能吗?我该怎么做?
我的另一个想法是创建一个浏览器队列,这样如果一个驱动程序什么也不做,它将由一个worker和一个来自队列的id_来执行,以便执行一个新进程.但我对多处理完全不熟悉,实际上不知道怎么写这个.
我感谢您的帮助.
您可以尝试在worker中实例化浏览器:
def worker(queue):
browser = webdriver.Chrome()
try:
while True:
id_ = queue.get(True)
obj = ReviewID(id_)
obj.search(browser)
if obj.exists(browser):
print(obj.get_url(browser))
else:
print("Nothing")
finally:
brower.quit()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2793 次 |
| 最近记录: |