I have been developing a web service in tornado for the last few months, in my test environment to run the service I use:
python index.py
index.py is my tornado application handler that listens on port 8001. I then request from the web service using http://localhost:8001
. I am now deploying my test environment to a staging environment that should mirror production. How do I go about running tornado in production? I'm guessing I need to create some sort of daemon for the application but I have no idea where to start!
There are a few tools you can use.
First, Supervisord
Supervisord is a "process control system", you configure your processes and let Supervisor manage them, it'll restart them if they fail, make managing them easier and keep them running in the background
Here's a sample supervisor config file
With that config, Supervisor will start 4 (
numprocs
) instances of index.py on ports 14001-14004 (numprocs_start
). We pass the--PORT=%(process_num)s
to get each process to start on a different port. You should changenumprocs
andnumprocs_start
to suit your environment/equipment. Generally we run 2xCPU cores processes (so a quad core processor would have 8 processes) but that can vary hugely based on what your processes do and how much blocking there is in your code.Next, configure NGINX to forward requests to your site
That config should be modified dependent on your application and the way it works, that is a very minimal configuration and will almost certainly need expanding on but that should be enough to get you started