-->

Tornado Python as daemon

2020-02-29 21:44发布

问题:

I have my code written with tornado and I want to make it work pretty much like apache or nginx, that is

  1. It must keep listening to the port even when I close the shell.
  2. It must start automatically on system restart

I have tried nohup command to make it work even when I close the shell. It works. But I am wondering if there is a cleaner option available for the same?

回答1:

Taken from the official documentation here.

Overview

Most Tornado apps are run as single processes. For production, this usually means a fairly straight-forward combination of external process management and proxying. Here are some gathered best practices/resources.

Development

When debug mode is enabled, templates are not cached and the app will automatically restart during development. This will fail if a Python syntax error occurs, however. (This can be worked around w/ some additional code or by using Supervisor in development)

You might want to run your app from a terminal multiplexer like screen or tmux for more flexibility in leaving things running and tracing fatal errors.

Instrumentation

Production

Typically in production, multiple tornado app processes are run (at least one per core) with a frontend proxy. Tornado developer bdarnell has a tornado-production-skeleton illustrating this using Supervisor (process management) and nginx (proxying).

Process Management

Traditionally, Tornado apps are single-process and require an external process manager, however HTTPServer can be run with multiple processes. Additionally, there are a couple additional helpers for helping out with managing multiple processes.

Supervisor
  • Managing multiple Pylons apps with Supervisor - an excellent tutorial for getting started with Supervisor
  • Deploy tornado application - short tutorial w/ Supervisor and nginx setup walkthrough
  • supervisord-example.conf - this is an example cfg for running 4 tornado instances
  • Nginx Supervisod Module - this module allows nginx to start/stop backends on demand
Daemonizing
  • start-stop-daemon example - if you are running a standard Linux system this is an easy way to daemonize your Tornado app
  • Upstart example - Upstart is built into Ubuntu and can respawn crashed instances.
Tornado Multi-Process

As mentioned above, Tornado's HTTPServer can be configured for both multiple processes on a single or multiple sockets.

Proxying

The official docs includes an example for running nginx as a load balancing proxy and for serving static files.

Deployment

  • Deploying Python web (Tornado) applications to multiple servers - short discussion on using a Load Balancer for live migration w/o downtime
  • Rolling Deployment w/ Fabric
  • Python Deployment Anti-Patterns
  • buedafab - a nice collection of Fabric deployment scripts w/ EC2


回答2:

I am using Python 3 and supervisord is not support python3 currently. My solution is native

create a new file as "my_tornado_app.service" on "/etc/systemd/system/"

and content

[Unit]
Description=MyTornadoApp1.0
[Service]
ExecStart=/usr/bin/python3 /var/vhosts/mytornado-app/app.py
User=www-data
Restart=on-failure
[Install]
WantedBy=multi-user.target

Now:

sudo systemctl daemon-reload
sudo systemctl enable my_tornado_app.service.service
sudo systemctl start my_tornado_app.service.service

And working!



回答3:

In short: Use supervisord and/or nginx.

Check this link out: http://blog.thisisfeifan.com/2012/06/deploy-tornado-application.html