簡體   English   中英

在 Python Quart 中獲取同步代碼的結果

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM