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.

Uvicorn cannot be shutdown programmatically

See original GitHub issue

There is no documented way to shutdown uvicorn in python:

ex:

instance = uvicorn.run("example:app", host="127.0.0.1", port=5000, log_level="info")
instance.shutdown()

How do we shutdown uvicorn?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

16reactions
florimondmancacommented, Aug 15, 2020

Hi,

Not documented indeed, but a multithreaded approach should do…

import contextlib
import time
import threading
import uvicorn

class Server(uvicorn.Server):
    def install_signal_handlers(self):
        pass

    @contextlib.contextmanager
    def run_in_thread(self):
        thread = threading.Thread(target=self.run)
        thread.start()
        try:
            while not self.started:
                time.sleep(1e-3)
            yield
        finally:
            self.should_exit = True
            thread.join()

config = Config("example:app", host="127.0.0.1", port=5000, log_level="info")
server = Server(config=config)

with server.run_in_thread():
    # Server started.
    ...
# Server stopped.

Very handy to run a live test server locally using a pytest fixture…

# conftest.py
import pytest

@pytest.fixture(scope="session")
def server():
    server = ...
    with server.run_in_thread():
        yield
4reactions
plazmakekscommented, Oct 29, 2020

I’m following @florimondmanca s approach to run pact tests on the provider side. However when using this i get the following error from uvicorn’s Server.run method:

RuntimeError: There is no current event loop in thread 'Thread-7'.

Any idea on that? Happens with python 3.6.9 as well as 3.7.7.

another edit:

I got it to work using

config = Config("example:app", host="127.0.0.1", port=5000, log_level="info", loop="asyncio")

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uvicorn cannot be shutdown programmatically #1103 - GitHub
Just run await server.serve() in a task and cancel that task when desired, then await server.shutdown() . It's a bit odd that cancelling...
Read more >
What is the best way to stop Uvicorn server programmatically?
In Docker, I run uvicorn with bootstrap.sh & command line. In code there is a condition about public key file, if exception occurs,...
Read more >
Server Behavior - Uvicorn
Uvicorn handles process shutdown gracefully, ensuring that connections are properly finalized, and all tasks have run to completion. During a shutdown period ...
Read more >
tiangolo/fastapi - Gitter
Hi all, Is there a way to trigger FastAPI to shutdown programmatically? Does Uvicorn have a shutdown(), or stop() function?
Read more >
Developers - Uvicorn cannot be shutdown programmatically -
There is no documented way to shutdown uvicorn in python: · Overview · Backers (0) · Updates ...
Read more >

github_iconTop Related Medium Post

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