报错信息:python38\lib\asyncio\windows_events.py", line 314 run_forever assert self._self_reading_future is None 解决如下: 需要引入nest_asyncio,代码如下: import nest_asyncio nest_asyncio. apply() 封装执行多个异步
报错信息:python38\lib\asyncio\windows_events.py", line 314 run_forever assert self._self_reading_future is None
解决如下:
需要引入nest_asyncio,代码如下:
import nest_asyncio
nest_asyncio.apply()
封装执行多个异步方法并返回结果
import asyncioimport nest_asyncio
from tornado.platform.asyncio import to_asyncio_future
nest_asyncio.apply()
async def asyncio_all_task(*fuc_list):
'''
执行多个异步方法
'''
tasks=[]
for t in fuc_list:
tasks.append(asyncio.ensure_future(t))
result= asyncio.get_event_loop().run_until_complete(to_asyncio_future(asyncio.gather(*tasks)))
# asyncio.get_event_loop().run_until_complete(asyncio.wait(tasks))
return result
def asyncio_all_task2(*fuc_list):
result=[]
loop = asyncio.get_event_loop()
res= loop.run_until_complete(asyncio.wait(fuc_list))
for r in res[0]:
result.append(r._result)
return result