Skip to content Skip to sidebar Skip to footer

How To Use Event Loop Created By Uvicorn?

I am using uvicorn and I need to use the existing event loop. I'm using the following command: loop = asyncio.get_event_loop() But when I use this line, the code get stuck. But if

Solution 1:

You can read this problem @ https://github.com/encode/uvicorn/issues/706

Basically you have to create your own event loop and pass it to uvicorn.

import asyncio
from uvicorn import Config, Server


asyncdefapp(scope, receive, send):
    await send({
        'type': 'http.response.start',
        'status': 200,
        'headers': [
            [b'content-type', b'text/plain'],
        ]
    })
    await send({
        'type': 'http.response.body',
        'body': b'Hello, world!',
    })


loop = asyncio.new_event_loop()

config = Config(app=app, loop=loop)
server = Server(config)
loop.run_until_complete(server.serve())

Post a Comment for "How To Use Event Loop Created By Uvicorn?"