Getting the result of synchronous code in Python Quart

606 views Asked by At

I have an asynchronous route in Quart in which I have to run a block of code that is synchronous. According to the documentation I should use run_sync from quart.utils to ensure that synchronous function does not block the event loop.

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"

However the print(result) returns <function sync_processor at 0x742d18a0>. How can I get the request object instead of the <function sync_processor at 0x742d18a0>.

1

There are 1 answers

0
pgjones On

You are missing an await as run_sync wraps the function with a coroutine function that you then need to call, i.e. result = await run_sync(sync_processor)() or in full,

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"