gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:50:33 26 4
gpt4 key购买 nike

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

关于python-3.x - 如何使用不支持异步的库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38536681/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com