如何使用不支持异步的库?

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

我正在从 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)