python asyncio并发编程(5)

2020-01-21  本文已影响0人  TheRightPath1

ThreadPoolExecutor和asyncio完成阻塞IO请求

loop = asyncio.get_event_loop()
tasks = list()
executor = ThreadPoolExecutor()
for i in range(20):
    tasks.append(i)
# run_in_executor方法用于运行线程池,第一个参数是线程池对象,第二个参数是阻塞函数名,第三个参数是函数参数
loop.run_in_executor(executor, inp, i)
loop.run_until_complete(asyncio.wait(tasks))
上一篇下一篇

猜你喜欢

热点阅读