question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get uvicorn event loop?

See original GitHub issue

I run uvicorn server programmatically:

if __name__ == '__main__':
    uvicorn.run(
        'app:app',
        host='127.0.0.1',
        port=8000,
        log_level='info',
        loop='asyncio'
    )

How can I get the created asyncio loop to reuse it for aiogram bot?

I have bot.py file for telegram bot and app.py file for starlette application running on uvicorn. When I’m trying to call asyncio.get_running_loop() in bot.py I gets the error that says that there is no running event loops at this time.

Is it even able to create asyncio loop manually and pass it to uvicorn.run function?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

17reactions
euri10commented, Jul 1, 2020
import asyncio
from uvicorn import Config, Server


async def app(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())
2reactions
vitaliy-diachkovcommented, Jul 1, 2020

I’ve actually find the solution by getting loop instance in on_startup() method of my Starlette application, but making user able to pass his loop instance can be a good impovement.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use event loop created by uvicorn? - Stack Overflow
Basically you have to create your own event loop and pass it to uvicorn. import asyncio from uvicorn import Config, Server async def ......
Read more >
Uvicorn
the event loop uvloop will be installed and used if possible. · uvloop is a fast, drop-in replacement of the built-in asyncio event...
Read more >
ASGI Event Loop Gotcha - Rob Blackbourn
Event () , via a web page served by the Uvicorn ASGI server, using the bareASGI framework. import asyncio import urllib.parsefrom bareasgi import...
Read more >
tiangolo/fastapi - Gitter
I'm using https://github.com/tiangolo/uvicorn-gunicorn-docker as the base ... time.sleep will block the event loop, you need await asyncio.sleep in an async ...
Read more >
fastapi asyncio.run() cannot be called from a running event loop
from fastapi import FastAPI import uvicorn app = FastAPI() @app.get("/") async ... This is due to Jupyter already running an event loop, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found