Skip to main content
 首页 » 编程设计

python之可以或如何在 Google Cloud Functions 上使用 Python asyncio

2024年12月31日9itcoder

是否可以在 Google Cloud Functions 上使用 Python 的 asyncio?

async def handle_someting(): 
    # do something 
    some = await aiofunc() 
 
# Do I need the code below? 
loop = asyncio.get_event_loop() 
loop.run_until_complete(handle_someting()) 
loop.close() 

请您参考如下方法:

当然,您只需要从主要部署函数运行异步函数(在这里,您将部署 test 函数):

import asyncio 
 
async def foo(): 
    return 'Asynchronicity!' 
 
async def bar(): 
    return await foo() 
 
def test(request): 
    return asyncio.run(bar())