简体   繁体   English

多处理和 Selenium Python

[英]Multiprocessing and Selenium Python

I have 3 drivers (Firefox browsers) and I want them to do something in a list of websites.我有 3 个驱动程序(Firefox 浏览器),我希望它们在网站列表中do something

I have a worker defined as:我有一个工人定义为:

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")

So the worker will just acces to a queue that contains the ids and use the browser to do something.因此,worker 将只访问包含 id 的队列并使用浏览器执行某些操作。

I want to have a pool of workers so that as soon as a worker has finished using the browser to do something on the website defined by id_, then it immediately starts to work using the same browser to do something on the next id_ found in queue.我想要一个工作人员池,以便工作人员完成使用浏览器在 id_ 定义的网站上执行某些操作后,立即开始使用相同的浏览器在队列中找到的下一个 id_ 上执行某些操作. I have then this:然后我有这个:

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)

Here I have a problem, I don't know how to define my workers so that they are in the pool.这里我有一个问题,我不知道如何定义我的工人,以便他们在池中。 To each driver I need to assign a worker, and all the workers share the same queue of ids.我需要为每个驱动程序分配一个工作人员,并且所有工作人员共享相同的 ID 队列。 Is this possible?这可能吗? How can I do it?我该怎么做?

Another idea that I have is to create a queue of browsers so that if a driver is doing nothing, it is taken by a worker, along with an id_ from the queue in order to perform a new process.我的另一个想法是创建一个浏览器队列,这样如果驱动程序什么都不做,它就会被工作人员获取,以及队列中的 id_ 以执行新进程。 But I'm completely new to multiprocessing and actually don't know how to write this.但是我对多处理完全陌生,实际上不知道如何编写它。

I appreciate your help.我很感激你的帮助。

You could try instantiating the browser in the worker:您可以尝试在 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()

I have 3 drivers (Firefox browsers) and I want them to do something in a list of websites.我有3个驱动器(火狐浏览器),我希望他们do something在网站列表。

I have a worker defined as:我有一个工人定义为:

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")

So the worker will just acces to a queue that contains the ids and use the browser to do something.因此,工作人员将仅访问包含ID的队列,然后使用浏览器执行某些操作。

I want to have a pool of workers so that as soon as a worker has finished using the browser to do something on the website defined by id_, then it immediately starts to work using the same browser to do something on the next id_ found in queue.我想拥有一个工作人员池,以便当一个工作人员完成使用浏览器在id_所定义的网站上做某事后,它立即开始使用同一浏览器在队列中找到的下一个id_上做某事。 I have then this:然后我有这个:

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)

Here I have a problem, I don't know how to define my workers so that they are in the pool.在这里,我有一个问题,我不知道如何定义我的工人,以便他们在游泳池中。 To each driver I need to assign a worker, and all the workers share the same queue of ids.我需要为每个驱动程序分配一个工作程序,并且所有工作程序共享相同的ID队列。 Is this possible?这可能吗? How can I do it?我该怎么做?

Another idea that I have is to create a queue of browsers so that if a driver is doing nothing, it is taken by a worker, along with an id_ from the queue in order to perform a new process.我的另一个想法是创建一个浏览器队列,这样,如果驱动程序什么都不做,它就会被工作人员以及队列中的id_占用,以执行新的过程。 But I'm completely new to multiprocessing and actually don't know how to write this.但是我是多处理技术的新手,实际上不知道该怎么写。

I appreciate your help.我感谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM