Reputation: 360
I'm moving from flask to aiohttp and I need to execute some queries in Oracle db, which doesn't support async. So I'm wondering how to do that in aiohttp?
How about this?
Or there's other (right) way to do that?
Thanks in advance!
Upvotes: 3
Views: 1841
Reputation: 13415
The loop.run_in_executor coroutine does exactly that:
result = await loop.run_in_executor(executor, sync_fn, *args)
Using your example:
executor = ThreadPoolExecutor(max_workers=1)
async def hello(request):
param1, param2 = get_params(request)
result = await app.loop.run_in_executor(executor, sync_fn, param1, param2)
return web.Response(text=result)
Upvotes: 7