Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python多处理与wait的连续处理_Python_Selenium_Async Await_Multiprocessing_Python 3.5 - Fatal编程技术网

Python多处理与wait的连续处理

Python多处理与wait的连续处理,python,selenium,async-await,multiprocessing,python-3.5,Python,Selenium,Async Await,Multiprocessing,Python 3.5,我正在使用一个基于事件的系统,使用新的Python3.5协程并等待。我注册事件,这些事件由系统调用 @event aysnc def handleevent(args): # handle the event 我需要初始化一些类来处理这项工作(耗时)。然后调用实例方法,这也很耗时(它们实际上使用selenium浏览某些站点) 理想情况下,我希望类似下面的代码 # supposedly since this is multiprocessing this is a different d

我正在使用一个基于事件的系统,使用新的Python3.5协程并等待。我注册事件,这些事件由系统调用

@event
aysnc def handleevent(args):
    # handle the event
我需要初始化一些类来处理这项工作(耗时)。然后调用实例方法,这也很耗时(它们实际上使用selenium浏览某些站点)

理想情况下,我希望类似下面的代码

# supposedly since this is multiprocessing this is a different driver per process
driver = None
def init():
    # do the heavy initialization here
    global driver
    driver = webdriver.Chrome()

def longworkmethod():
    ## need to return some data
    return driver.dolongwork()

class Drivers:
""" A class to handle async and multiprocessing"""
    def __init__(self, numberOfDrivers):
        self.pool = multiprocessing.Pool(processes=numberOfDrivers, initializer=init)       

    async def dowork(self, args):
        return self.pool.apply_async(longworkmethod, args=args)


### my main python class
drivers = Drivers(5)

@event
aysnc def handleevent(args):
    await drivers.dowork(args)

@event
aysnc def quit(args):
    ## do cleanup on drivers
    sys.exit(0)
这段代码不起作用,但我已经尝试了许多不同的方法,似乎没有一种能够实现我想要的


它不一定是这种精确的形式,但我如何将等待和协同路由与需要多处理的程序混合使用呢?

虽然从技术上讲,没有任何东西会限制您混合使用
异步
多处理
,但我建议避免这样做。这将增加很多复杂性,因为最终每个线程都需要一个事件循环,来回传递信息将很棘手。用一个或另一个

asyncio
为在另一个线程中运行任务提供函数,例如。看看这些答案

  • (在协同程序中调用selenium)

或者,您可以只使用
多处理
,因为selenium有一个阻塞(非异步)接口,但是听起来您的一些代码已经在使用
asyncio
,所以可能要坚持上面的方法。

我无法避免这种混合,因为我需要selenium(多处理),我使用的事件系统是严格异步的。但我会查看这些链接。看看我能不能让它们工作。selenium是阻塞的,但这并不意味着你需要多处理。只需使用
run\in\u executor
作为阻塞位,并将所有其他内容保留在主线程/事件循环中。因此,这些链接似乎很有帮助。我应该使用run_in_executor来执行单个昂贵的调用,比如“get”,还是可以使用多个昂贵的调用来执行大型昂贵的函数?会有区别吗?@leftsync这取决于你的通话结构。它们可以同时运行还是需要按顺序运行?您可以支持多少线程?硒线程安全吗?