Why coroutines cannot be used with run_in_executor?

后端 未结 2 742
春和景丽
春和景丽 2021-02-05 19:22

I want to run a service that requests urls using coroutines and multithread. However I cannot pass coroutines to the workers in the executor. See the code below for a minimal ex

2条回答
  •  渐次进展
    2021-02-05 20:00

    You have to create and set a new event loop in the thread context in order to run coroutines:

    import asyncio
    from concurrent.futures import ThreadPoolExecutor
    
    
    def run(corofn, *args):
        loop = asyncio.new_event_loop()
        try:
            coro = corofn(*args)
            asyncio.set_event_loop(loop)
            return loop.run_until_complete(coro)
        finally:
            loop.close()
    
    
    async def main():
        loop = asyncio.get_event_loop()
        executor = ThreadPoolExecutor(max_workers=5)
        futures = [
            loop.run_in_executor(executor, run, asyncio.sleep, 1, x)
            for x in range(10)]
        print(await asyncio.gather(*futures))
        # Prints: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    
    if __name__ == '__main__':
        loop = asyncio.get_event_loop()
        loop.run_until_complete(main())
    

提交回复
热议问题