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))
网友评论