您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python-asyncio TypeError:对象字典不能在'await'表达式中使用

python-asyncio TypeError:对象字典不能在'await'表达式中使用

只能async def等待异步(用定义)函数。整个想法是,此类函数以特殊的方式编写,因此可以在await不阻塞事件循环的情况下运行()它们。

如果要从def需要花费大量时间才能执行的通用(用定义)函数获取结果,可以使用以下选项:

通常,您要选择第二个选项。

这是如何做的例子:

import asyncio
import time
from concurrent.futures import ThreadPoolExecutor


_executor = ThreadPoolExecutor(1)


def sync_blocking():
    time.sleep(2)


async def hello_world():
    # run blocking function in another thread,
    # and wait for it's result:
    await loop.run_in_executor(_executor, sync_blocking)


loop = asyncio.get_event_loop()
loop.run_until_complete(hello_world())
loop.close()
python 2022/1/1 18:34:33 有225人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶