[英]Getting the result of synchronous code in Python Quart
我在 Quart 中有一个异步路由,我必须在其中运行一个同步代码块。 根据文档,我应该使用 quart.utils 中的 run_sync 来确保同步函数不会阻塞事件循环。
def sync_processor():
request = requests.get('https://api.github.com/events')
return request
@app.route('/')
async def test():
result = run_sync(sync_processor)
print(result)
return "test"
然而,打印(结果)返回 <function sync_processor at 0x742d18a0>。 如何获取请求对象而不是 <function sync_processor at 0x742d18a0>。
您错过了await
因为run_sync
用一个协程函数包装了该函数,然后您需要调用该函数,即result = await run_sync(sync_processor)()
或完整的,
def sync_processor():
request = requests.get('https://api.github.com/events')
return request
@app.route('/')
async def test():
result = await run_sync(sync_processor)()
print(result)
return "test"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.