Managing the uWSGI server

See also

If you are managing multiple apps or a high volume site, take a look at

Starting the server

Starting an uWSGI server is the role of the system administrator, like starting the Web server. It should not be the role of the Web server to start the uWSGI server – though you can also do that if it fits your architecture.

How to best start uWSGI services at boot depends on the operating system you use.

On modern systems the following should hold true. On “classic” operating systems you can use init.d/rc.d scripts, or tools such as Supervisor, Daemontools or inetd/xinetd.

SystemMethod
Ubuntu < 15.04Running uWSGI via Upstart (the official uwsgi package, available since Ubuntu 12.04 provides an init.d based solution. Read the README.)
Ubuntu >= 15.04Systemd
DebianSystemd
Arch LinuxSystemd
FedoraSystemd
OSXlaunchd
SolarisSMF

Signals for controlling uWSGI

You can instruct uWSGI to write the master process PID to a file with the safe-pidfile option.

The uWSGI server responds to the following signals.

SignalDescriptionConvenience command
SIGHUPgracefully reload all the workers and the master process—reload
SIGTERMbrutally reload all the workers and the master process(use —die-on-term to respect the convention of shutting down the instance)
SIGINTimmediately kill the entire uWSGI stack—stop
SIGQUITimmediately kill the entire uWSGI stack
SIGUSR1print statistics
SIGUSR2print worker status or wakeup the spooler
SIGURGrestore a snapshot
SIGTSTPpause/suspend/resume an instance
SIGWINCHwakeup a worker blocked in a syscall (internal use)
SIGFPEgenerate C traceback
SIGSEGVgenerate C traceback

Note: there are better ways to manage your instances than signals, as an example the master-fifo is way more robust.

Reloading the server

When running with the master process mode, the uWSGI server can be gracefully restarted without closing the main sockets.

This functionality allows you patch/upgrade the uWSGI server without closing the connection with the web server and losing a single request.

When you send the SIGHUP to the master process it will try to gracefully stop all the workers, waiting for the completion of any currently running requests.

Then it closes all the eventually opened file descriptors not related to uWSGI.

Lastly, it binary patches (using execve()) the uWSGI process image with a new one, inheriting all of the previous file descriptors.

The server will know that it is a reloaded instance and will skip all the sockets initialization, reusing the previous ones.

Note

Sending the SIGTERM signal will obtain the same result reload-wise but will not wait for the completion of running requests.

There are several ways to make uWSGI gracefully restart.

  1. # using kill to send the signal
  2. kill -HUP `cat /tmp/project-master.pid`
  3. # or the convenience option --reload
  4. uwsgi --reload /tmp/project-master.pid
  5. # or if uwsgi was started with touch-reload=/tmp/somefile
  6. touch /tmp/somefile

Or from your application, in Python:

  1. uwsgi.reload()

Or in Ruby,

  1. UWSGI.reload

Stopping the server

If you have the uWSGI process running in the foreground for some reason, you can just hit CTRL+C to kill it off.

When dealing with background processes, you’ll need to use the master pidfile again. The SIGINT signal will kill uWSGI.

  1. kill -INT `cat /tmp/project-master.pid`
  2. # or for convenience...
  3. uwsgi --stop /tmp/project-master.pid

The Master FIFO

Starting from uWSGI 1.9.17, a new management system has been added using unix named pipes (fifo): The Master FIFO