首页 > 解决方案 > 可以或如何在 Google Cloud Functions 上使用 Python asyncio?

问题描述

是否可以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()

标签: pythongoogle-cloud-platformgoogle-cloud-functionspython-asyncio

解决方案


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

import asyncio

async def foo():
    return 'Asynchronicity!'

async def bar():
    return await foo()

def test(request):
    return asyncio.run(bar())

推荐阅读