简体   繁体   English

如何在ProcessPoolExecutor中使用asyncio

[英]How to use asyncio with ProcessPoolExecutor

I am searching for huge number of addresses on web, I want to use both asyncio and ProcessPoolExecutor in my task to quickly search the addresses. 我正在网上搜索大量地址,我想在任务中同时使用asyncio和ProcessPoolExecutor来快速搜索地址。

    async def main():
        n_jobs = 3
        addresses = [list of addresses]
        _addresses = list_splitter(data=addresses, n=n_jobs)
        with ProcessPoolExecutor(max_workers=n_jobs) as executor:
             futures_list = []
             for _address in _addresses:
                futures_list +=[asyncio.get_event_loop().run_in_executor(executor, execute_parallel, _address)]

                for f in tqdm(as_completed(futures_list, loop=asyncio.get_event_loop()), total=len(_addresses)):
                results = await f

asyncio.get_event_loop().run_until_complete(main())

expected: I want to execute_parallel function should run in parallel. 预期的:我想execute_parallel函数应该并行运行。

error: 错误:

    Traceback (most recent call last):
  File "/home/awaish/danamica/scraping/skraafoto/aerial_photos_scraper.py", line 228, in <module>
    asyncio.run(main())
  File "/usr/local/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
    return future.result()
  File "/home/awaish/danamica/scraping/skraafoto/aerial_photos_scraper.py", line 224, in main
    results = await f
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 533, in _wait_for_one
    return f.result()  # May raise f.exception().
TypeError: can't pickle coroutine objects

I'm not sure I'm answering the correct question, but it appears the intent of your code is to run your execute_parallel function across several processes using Asyncio. 我不确定我回答的是正确的问题,但是看来您的代码的目的是使用Asyncio在多个进程中运行execute_parallel函数。 As opposed to using ProcessPoolExecutor, why not try something like using a normal multiprocessing Pool and setting up separate Asyncio loops to run in each. 与使用ProcessPoolExecutor相反,为什么不尝试使用普通的多处理池并设置单独的Asyncio循环在每个循环中运行。 You might set up one process per core and let Asyncio work its magic within each process. 您可能会为每个内核设置一个进程,然后让Asyncio在每个进程中发挥其魔力。

async def run_loop(addresses):
    loop = asyncio.get_event_loop()
    loops = [loop.create_task(execute_parallel, address) for address in addresses]
    loop.run_until_complete(asyncio.wait(loops))

def main():
    n_jobs = 3
    addresses = [list of addresses]
    _addresses = list_splitter(data=addresses, n=n_jobs)
    with multiprocessing.Pool(processes=n_jobs) as pool:
        pool.imap_unordered(run_loop, _addresses)

I've used Pool.imap_unordered with great success, but depending on your needs you may prefer Pool.map or some other functionality. 我使用Pool.imap_unordered取得了很大的成功,但是根据您的需要,您可能更喜欢Pool.map或其他功能。 You can play around with chunksize or with the number of addresses in each list to achieve optimal results (ie, if you're getting a lot of timeouts you may want to reduce the number of addresses being processed concurrently) 您可以尝试使用块大小或每个列表中的地址数量来获得最佳结果(即,如果超时很多,您可能希望减少同时处理的地址数量)

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

相关问题 如何使用asyncio减少ProcessPoolExecutor的cpu使用率? - How to reduce ProcessPoolExecutor's cpu usage with asyncio? 如何使用 ProcessPoolExecutor 和 asyncio 异步运行阻塞任务? - How to run a blocking task asynchronously with ProcessPoolExecutor and asyncio? Python 3 asyncio和GIL(如何使用所有cpu核心-除了ProcessPoolExecutor以外的任何其他选项)? - Python 3 asyncio and GIL (how to use all cpu cores - any other options than ProcessPoolExecutor)? AsyncIO使用ProcessPoolExecutor在执行程序中运行 - AsyncIO run in executor using ProcessPoolExecutor 如何在 Python 中正确使用 Queue 和 ProcessPoolExecutor? - How to use Queue correctly with ProcessPoolExecutor in Python? 如何使用 asyncio 和 concurrent.futures.ProcessPoolExecutor 在 Python 中终止长时间运行的计算(CPU 绑定任务)? - How to terminate long-running computation (CPU bound task) in Python using asyncio and concurrent.futures.ProcessPoolExecutor? 如何在异步中使用线程? - How to use thread in asyncio? 如何在多线程中使用 asyncio? - How to use asyncio with multithreading? 将asyncio与多人ProcessPoolExecutor结合使用并进行异步 - Combining asyncio with a multi-worker ProcessPoolExecutor and for async 将asyncio与多工作程序ProcessPoolExecutor结合使用 - Combining asyncio with a multi-worker ProcessPoolExecutor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM