【问题标题】:Python - What is the right way to run functions in asyncio?Python - 在 asyncio 中运行函数的正确方法是什么?
【发布时间】:2020-07-06 21:57:42
【问题描述】:

我正在使用 FastApi 并且有一个端点。

我有两个长时间运行的函数,我想使用 asyncio 同时运行它们

因此,我创建了两个函数:

async def get_data_one():
    return 'done_one'

async def get_data_two():
    return 'done_two'

这些函数从外部网络服务获取数据。

我想同时执行它们,所以我创建了另一个函数来执行它:

async def get_data():
    loop = asyncio.get_event_loop()
    asyncio.set_event_loop(loop)
    task_1 = loop.create_task(get_data_one)
    task_2 = loop.create_task(get_data_two)
    tasks = (task_1, task_2)
    first, second = loop.run_until_complete(asyncio.gather(*tasks))
    loop.close()

    # I will then perform cpu intensive computation on the results
    # for now - assume i am just concatenating the results
    return first + second

最后,我有我的端点:

@app.post("/result")
async def get_result_async():
    r = await get_data()

    return r

即使是这个简单的例子也中断了,当我到达端点时,我得到了以下异常:

RuntimeError:此事件循环已在运行 错误:从未检索到_GatheringFuture 异常 未来:<_gatheringfuture> AttributeError: 'function' 对象没有属性 'send'

这是一个简化的代码,但我真的很感激如何以正确的方式来做。

【问题讨论】:

    标签: python python-3.x python-asyncio fastapi


    【解决方案1】:

    在 FastAPI 上下文中,您永远不需要运行异步循环;只要您的服务器进程存在,它就会一直运行。

    因此,您只需要

    import asyncio
    
    
    async def get_data_one():
        return "done_one"
    
    
    async def get_data_two():
        return "done_two"
    
    
    async def get_data():
        a, b = await asyncio.gather(get_data_one(), get_data_two())
        return a + b
    
    
    #@route decorator here...
    async def get_result_async():
        r = await get_data()
        print("Get_data said:", r)
        return r
    
    
    # (this is approximately what is done under the hood,
    #  presented here to make this a self-contained example)
    asyncio.run(get_result_async())
    

    【讨论】:

    • 我是否需要在 get_data 函数中锁定任何数据?或者异步将在其自己的“容器”中运行每个请求,我无需担心使代码异步/线程安全?
    • 它们不是独立运行的——如果你有全局状态,你需要像处理线程一样处理它。
    【解决方案2】:

    很简单:

    async def get_data():
        first, second = await asyncio.gather(
            get_data_one(),
            get_data_two(),
        )
    
        return first + second
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-22
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多