Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to deploy new code with Tornado/Python without restarting the server?

Tags:

python

tornado

I've recently started to experiment with Python and Tornado web server/framework for web development. Previously, I have used PHP with my own framework on a LAMP stack. With PHP, deploying updated code/new code is as easy as uploading it to the server because of the way mod_php and Apache interact.

When I add new code or update code in Python/Tornado, do I need to restart the Tornado server? I could see this being problematic if you have a number of active users.

(a) Do I have to restart the server, or is there another/better way?

(b) If so, how can I avoid users being disconnected/getting errors/etc. while it's restarting (which could take a few seconds)?

[One possible thought is to use the page flipping paradigm with Nginx pointing to a server, launch the new server instance with updated code, redirect Nginx there and take down the original server...?]

like image 258
Kyle Johnson Avatar asked Nov 10 '11 21:11

Kyle Johnson


2 Answers

It appears the best method is to use Nginx with multiple Tornado instances as I alluded to in my original question and as Cole mentions. Nginx can reload its configuration file on the fly . So the process looks like this:

  1. Update Python/Tornado web application code
  2. Start a new instance of the application on a different port
  3. Update the configuration file of Nginx to point to the new instance (testing the syntax of the configuration file first)
  4. Reload the Nginx configuration file with a kill -HUP command
  5. Stop the old instance of Python/Tornado web server

A couple useful resources on Nginx regarding hot-swapping the configuration file:

https://calomel.org/nginx.html (in "Explaining the directives in nginx.conf" section) http://wiki.nginx.org/CommandLine (in "Loading a New Configuration Using Signals" section)

like image 157
Kyle Johnson Avatar answered Nov 14 '22 18:11

Kyle Johnson


Use HAProxy or Nginx and proxy to multiple Tornado processes, which you can then restart one by one. The Tornado docs cover Nginx, but it doesn't support websockets, so if you're using them you'll need HAProxy.

like image 38
Cole Maclean Avatar answered Nov 14 '22 19:11

Cole Maclean