我何时应该在Python中使用异步/等待?

时间:2018-09-27 00:30:54

标签: python-3.x performance asynchronous

元上下文:

我正在使用aiohttp构建一个api。由于它是一个异步框架,因此我必须使用异步来定义处理程序。示例:

async def index_handler(request):
    return web.Response(text="Hello, world")

app = web.Application()
app.router.add_route("GET", "/", index_handler)

代码上下文:

在开发过程中,我发现自己处于嵌套函数调用的情况,如下所示:

def bar():
    return "Hi"

async def foo():
    return bar()

await foo()

在考虑性能时,我不知道是否也应该异步执行这些嵌套函数。示例:

async def bar()
    return "Hi"

async def foo():
    return await bar()

await foo()

问题:

进行嵌套函数调用以优化性能的最佳方法是什么?始终使用异步/等待还是始终使用同步?这有什么区别吗?

0 个答案:

没有答案
相关问题