Package: django-webserver
django-webserverRun production webservers such aspyuwsgi(aka uWSGI) orgunicornas a Django management command.UsageInstall a variant:pip install django-webserver[pyuwsgi]orpip install django-webserver[gunicorn]orpip install django-webserver[uvicorn]  # Python 3.5+ onlyorpip install django-webserver[waitress]Add toINSTALLED_APPS:INSTALLED_APPS=[# ..."django_webserver",# ...]Run:manage.py pyuwsgi --http=:8000 ...ormanage.py gunicornormanage.py uvicornormanage.py waitress --port=8000ConfigurationWith pyuwsgi(py)uWSGI docsPyuwsgi already knows the Python interpreter and virtualenv (if applicable) to use from the Django management command environment. By default, it will run with the following flags (usingsettings.WSGI_APPLICATIONto determine the module):--strict --need-app --module={derived}If you haveSTATIC_URLdefined with a local URL, it will also add--static-map, derived fromSTATIC_URLandSTATIC_ROOT.You can pass any additional arguments uWSGI accepts in from the command line.But uWSGI has a lot of flags, and many of them, you want every time you run the project. For that scenario, you can configure your own defaults using the optional setting,PYUWSGI_ARGS. Here's an example you might find helpful:PYUWSGI_ARGS=["--master","--strict","--need-app","--module".":".join(WSGI_APPLICATION.rsplit(".",1)),"--no-orphans","--vacuum","--auto-procname","--enable-threads","--offload-threads=4","--thunder-lock","--static-map","=".join([STATIC_URL.rstrip("/"),STATIC_ROOT]),"--static-expires","/* 7776000",]Don't forget to also set something like--socket=:8000or--http=:8000so your app listens on a port. Depending on your setup, it may make more sense to pass this in via the command line than hard-coding it in your settings.With gunicorngunicorn docsSame as the standard gunicorn configuration, but the application will be set for you fromsettings.WSGI_APPLICATION.Note: Unlike the other servers, you have to configure gunicorn with environment variables or viasys.argv. If you use it with Django'scall_command, keep in mind any additional arguments you pass will not be applied.With uvicornuvicorn docsSame as the standard uvicorn configuration, but the application will be set for you fromsettings.WSGI_APPLICATIONas well as--wsgi.With waitresswaitress docsSame as the standardwaitress-servearguments, but the application will be set for you fromsettings.WSGI_APPLICATION.Unlike the other servers, waitress is supported on Windows.Pre-warming Your AppDefault:WEBSERVER_WARMUP=TrueTypically, when a WSGI server starts, it will bind to the necessary portsthenimport/setup your application. On larger projects, it's normal for startup to take multiple seconds. During that time, it is unable to respond to incoming requests.To avoid that downtime, this app imports your WSGI modulebeforestarting the relevant server. If, for some reason this behavior is undesirable, you can setWEBSERVER_WARMUP = Falsein your settings.Running a Healthcheck at StartupThis is not enabled by default. It requiresWEBSERVER_WARMUP = True.WEBSERVER_WARMUP_HEALTHCHECK="/-/health/"Internally calls the provided URL prior to starting the server and exits with a failure if it does not return a200.It can be helpful to have your app exit immediately if it is unable to successfully respond to a healthcheck. Your process or container manager should immediately show the service failed instead of waiting for a load balancer or some other monitoring tool to notify catch the problem.MotivationIn some scenarios, it is beneficial to distribute a Django project with a single entrypoint for command-line interaction. This can come in handy when building Docker containers or self-contained Python apps with something likeshiv.Pre-warming the application and running a healthcheck can also open the door for some zero-downtime deployment scenarios that previously weren't possible due to the issues described in "Pre-warming your app". For example, you could use the--reuse-portoption in uWSGI or gunicorn to bring up a new version of your app on the same port, knowing it is already warmed-up and healthy. After a successful startup, the old version can safely be torn down without dropping any traffic.