python 3.x - 如何使用不支持异步的库?



我正在从flask转移到aiohttp,我需要在Oracle数据库中执行一些查询,它不支持异步。所以我想知道如何在aiohttp中做到这一点?

这个怎么样?

http://pastebin.com/nbWABbvK

或者还有其他(正确的)方法可以做到这一点?

提前感谢!

loop.run_in_executor协同程序正是这样做的:

result = await loop.run_in_executor(executor, sync_fn, *args)

使用您的示例:

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)

最新更新