Meta Context:
I'm building an api using aiohttp. Since it's an asynchronous framework, I have to use async to define handlers. Example:
async def index_handler(request): return web.Response(text="Hello, world") app = web.Application() app.router.add_route("GET", "/", index_handler) Code Context:
During development, I found myself in a situation where i have nested function calls like so:
def bar(): return "Hi" async def foo(): return bar() await foo() When I was thinking about performance, I didn't know whether or not I should do these nested functions all async as well. Example:
async def bar() return "Hi" async def foo(): return await bar() await foo() Question:
What is the best way to do nested function calls to optimize performance? Always use async/await or always use sync? Does this makes a difference?
async. Eg: waiting for any kind of IO. In your case it's the opposite: your "async-everywhere" solution is slower, sinceasyncdoes not come for free. That's another reason why every performance optimisation should come with before-after benchmarks.