How to Restart Uvicorn Server for Python FastAPI Projects

Uvicorn is an ASGI server that works with your async or sync applications, making it a popular choice for deploying and running asynchronous web apps developed in the modern frameworks such as FastAPI. Sometimes you may need to restart your uvicorn application due to various reasons like errors occurring during runtime, code changes requiring updates without complete service downtime, etc. This guide provides step-by-step instructions on how to safely stop a Uvicorn server and start it again.

Before proceeding with the restart process ensure that: * You have installed uvicorn (`pip install uvicorn`). Make sure your Python environment is active (or deactivated if not). * Your FastAPI project's main file, often `main.py`, has been configured to run as an ASGI application with Uvicorn server using the following command: ```shell uvicorn main:app --host localhost --port 8000 --reload --workers 1 # Add your desired host and port here, set `--reload` for automatic reloading on code changes. Set worker count according to expected traffic load (default is one). You can also use `.env` file with environment variables if needed. ``` * Your project does not have any live connections that rely solely on the uvicorn server; all such dependencies should be managed within their respective packages or services, and Uvicorn must handle those as well to maintain consistency in data processing when restarting it.

If your application is already running via uvicorn:

```shell $ ps aux | grep uvicorn /usr/local/bin/uvicorn main:app --host localhost --port 8000 --reload --workers=1 & your_user 4567 0.2 3.9g ... 00:00:00 myserver # Replace 'myserver' with your actual server name or command output, and adjust the PID accordingly. ```

```shell $ kill 4567 ``` This ensures no stale processes are left hanging when restarting your server as they might cause conflicts or issues. The '&' at the end allows multiple workers if set up in `main.py`. If a single worker is used, you can just remove `&` and proceed with starting uvicorn without specifying `--workers=1`, which defaults to one by default: ```shell $ kill 4567 # Use this step only when using more than one workers (if configured). Otherwise skip it. $ uvicorn main:app --host localhost --port 8000 --reload # This will start a fresh server instance without interfering with previous instances if any exist, allowing smooth operation and updates to be applied immediately upon request handling. You can specify the number of workers according to your application's needs for load balancing or scaling purposes: uvicorn main_app:app --host 0.0.0.0 --port 8000 # Replace `main_app` with relevant path and set host if required by external network setup, using '0.0.0.0' makes your server accessible externally but be cautious about security implications when exposed to the open internet: ```

For development environments where code changes are frequent and uvicorn should restart automatically without manual intervention, use this command with `--reload`: ```shell uvicorn main_app --host localhost --port 8000 --reload # Replace `main_app` as mentioned above. This will make the server reload on code changes in debug mode which can be useful for continuous integration and development workflows: ```
Quality, Reliability & Service
Thank You For Visiting
Brooks Computing Systems - Jacksonville
Visit https://bcs.archman.us