tornado先天對異步(no-bolocking)處理能力,很是適合做爲Web服務。tornado在linux平臺使用epoll來實現異步事件的處理,性能很是好。可是python作爲一個腳步語言,單進程執行,沒法利用多CPU,對當今的多核CPU是一個很大的浪費。爲提升性能,提升CPU利用率,通常會將tornado程序容許cup*n個。 怎樣才能放便啓動多個tornado程序呢,咱們能夠用supervisor來管理多個tornado應用。supervisor安裝很是方便html
easy_install supervisord
###安裝Nginxpython
groupadd www useradd -s /sbin/nologin -g www www mkdir -p /data/wwwroot chmod +w /data/wwwroot mkdir -p /data/wwwroot/logs chmod 755 /data/wwwroot/logs chown -R www:www /data/wwwroot wget http://www.nginx.org/download/nginx-1.3.11.tar.gz -P /data/soft/src/ tar zxvf /data/soft/src/nginx-1.3.11.tar.gz -C /data/soft/install cd /data/soft/install/nginx-1.3.11 yum install zlib pcre pcre-devel openssl ./configure \ --user=www\ --group=www\ --prefix=/etc/nginx\ --sbin-path=/etc/nginx/sbin/nginx\ --conf-path=/etc/nginx/conf/nginx.conf\ --with-http_stub_status_module\ --with-http_ssl_module\ --with-pcre\ make && make install vim /etc/init.d/nginx
加入如下內容linux
#!/bin/bash # # chkconfig: - 85 15 # description: Nginx is a World Wide Web server. # processname: nginx nginx=/etc/nginx/sbin/nginx conf=/etc/nginx/conf/nginx.conf case $1 in start) echo -n "Starting Nginx" $nginx -c $conf echo " done" ;; stop) echo -n "Stopping Nginx" killall -9 nginx echo " done" ;; test) $nginx -t -c $conf ;; reload) echo -n "Reloading Nginx" ps auxww | grep nginx | grep master | awk '{print $2}' | xargs kill -HUP echo " done" ;; restart) $0 stop $0 start ;; show) ps -aux|grep nginx ;; *) echo -n "Usage: $0 {start|restart|reload|stop|test|show}" ;; esac chmod +x /etc/init.d/nginx chkconfig --add nginx chkconfig nginx on service nginx start service nginx test mv /etc/nginx/conf/nginx.conf /etc/nginx/conf/nginx.conf_bak cp /data/soft/src/nginx.conf /etc/nginx/conf/nginx.conf vim /etc/nginx/conf/nginx.conf
yum install python-setuptools easy_install tornado
###安裝 supervisornginx
http://supervisord.org/configuration.html?highlight=program#program-x-section-settingsvim
easy_install supervisor
而後建立配置文件bash
echo_supervisord_conf > /etc/supervisord.conf vim /etc/supervisord.conf
最後面加上:app
[program:tornado_poll] command=python /home/wwwroot/app/app.py 80%(process_num)02d ;要執行的命令,這裏的「%(process_num)02d」會用2位精度的進程號替換,例如,第一個進程是8001,第二個進程是8002,以此類推,下同。 process_name=%(program_name)s-80%(process_num)02d ;process_name expr (default %(program_name)s) ;啓動的進程的名字,這裏的名字只是supervisor內部是別用,與你所啓動程序的進程名無關 numprocs=4 ; 啓動幾個tornado進程 directory=/home/wwwroot/app ; 運行前cd到此目錄 autostart=true ; supervisord守護程序啓動時自動啓動tornado autorestart=true ; supervisord守護程序重啓時自動重啓tornado user=www-data ; 運行程序前su到此用戶 redirect_stderr=true ; 將stderr重定向到stdout stdout_logfile=/home/wwwroot/logs/tornado-80%(process_num)02d.log stdout_logfile_maxbytes=500MB stdout_logfile_backups=50 stdout_capture_maxbytes=1MB stdout_events_enabled=false loglevel=warn
運行:異步
supervisord root@vps:/tmp# supervisorctl tornado_poll:tornado_poll-8000 RUNNING pid 11352, uptime 0:29:01 tornado_poll:tornado_poll-8001 RUNNING pid 11347, uptime 0:29:02 supervisor>#這裏能夠輸入控制命令
咱們能夠看到,上面顯示瞭如今正在運行的守護進程的信息,下面介紹幾個經常使用的控制命令ide
stop all #中止全部進程 stop tornado_poll:tornado_poll-8000 #中止運行在8000端口上的Tornado守護進程 stop tornado_poll:* #中止全部Tornado守護進程 supervisord supervisorctl reload
問題:tornado
* Starting Supervisor daemon manager... Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord. For help, use /usr/bin/supervisord -h ...fail!
解決辦法:
unlink /tmp/supervisor.sock
http://www.idndx.com/posts/ways-to-deploy-tornado-under-production-environment-using-supervisor.html
http://supervisord.org/ http://fendou.org/2011/09/23/supervisor-nginx-tornado/