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

Sag*_*nol 5 python python-asyncio google-cloud-platform google-cloud-functions

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

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()
Run Code Online (Sandbox Code Playgroud)

Dus*_*ram 5

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

import asyncio

async def foo():
    return 'Asynchronicity!'

async def bar():
    return await foo()

def test(request):
    return asyncio.run(bar())
Run Code Online (Sandbox Code Playgroud)

  • @Robino 这里的最后一个函数(`test`)是一个 Google Cloud Function。这个示例演示了如何将 asyncio 与云函数一起使用,就像OP所问的那样...... (3认同)