tornado执行asyncio异步报错run_forever assert self._self_reading_future is None怎么解决

2023-06-25,

本篇内容介绍了“tornado执行asyncio异步报错run_forever  assert self._self_reading_future is None怎么解决”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

报错信息: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 asyncio
import 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

“tornado执行asyncio异步报错run_forever  assert self._self_reading_future is None怎么解决”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注本站网站,小编将为大家输出更多高质量的实用文章!

《tornado执行asyncio异步报错run_forever assert self._self_reading_future is None怎么解决.doc》

下载本文的Word格式文档,以方便收藏与打印。