我在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"但是,打印(结果)在0x742d18a0>返回<函数sync_processor。如何在0x742d18a0>中获取请求对象而不是
发布于 2020-10-01 17:55:32
您缺少一个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"https://stackoverflow.com/questions/64121194
复制相似问题