How to change username
How to change username

Reputation: 360

How to work with libraries that don't support async?

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?

http://pastebin.com/nbWABbvK

Or there's other (right) way to do that?

Thanks in advance!

Upvotes: 3

Views: 1841

Answers (1)

Vincent
Vincent

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

Related Questions