Running uWSGI instances with Circus

Circus (https://circus.readthedocs.io/en/0.7/) is a process manager written inPython. It is very similar to projects like Supervisor, but with severaladditional features. Although most, if not all, of it’s functionalities have acounterpart in uWSGI, Circus can be used as a library allowing you to builddynamic configurations (and extend uWSGI patterns). This aspect is veryimportant and may be the real selling point of Circus.

Socket activation

Based on the venerable inetd pattern, Circus can bind to sockets and pass them to children.

Start with a simple Circus config (call it circus.ini):

  1. [circus]
  2. endpoint = tcp://127.0.0.1:5555
  3. pubsub_endpoint = tcp://127.0.0.1:5556
  4. stats_endpoint = tcp://127.0.0.1:5557
  5.  
  6. [watcher:dummy]
  7. cmd = uwsgi --http-socket fd://$(circus.sockets.foo) --wsgi-file yourapp.wsgi
  8. use_sockets = True
  9. send_hup = True
  10. stop_signal = QUIT
  11.  
  12. [socket:foo]
  13. host = 0.0.0.0
  14. port = 8888

run it with

  1. circusd circus.ini

(Better) Socket activation

If you want to spawn instances on demand, you will likely want to shut themdown when they are no longer used. To accomplish that use the –idle uWSGIoption.

  1. [circus]
  2. check_delay = 5
  3. endpoint = tcp://127.0.0.1:5555
  4. pubsub_endpoint = tcp://127.0.0.1:5556
  5. stats_endpoint = tcp://127.0.0.1:5557
  6.  
  7. [watcher:dummy]
  8. cmd = uwsgi --master --idle 60 --http-socket fd://$(circus.sockets.foo) --wsgi-file yourapp.wsgi
  9. use_sockets = True
  10. warmup_delay = 0
  11. send_hup = True
  12. stop_signal = QUIT
  13.  
  14. [socket:foo]
  15. host = 0.0.0.0
  16. port = 8888

This time we have enabled the master process. It will manage the –idle option, shutting down the instance if it isinactive for more than 60 seconds.