首先我們需要安裝gunicorn,gevent戳吝,supervisor浩销。在centos下使用pip install gunicorn,pip install gevent听哭。yum install supervisor慢洋。然后建立gunicorn的配置文件gun.py塘雳,代碼如下:
import gevent.monkey
import multiprocessing
gevent.monkey.patch_all()
bind = '0.0.0.0:9000'
# restart workers when code change, only use in development
#reload = True
preload_app = True
# debug when development and error when production
loglevel = 'error'
logfile = '/var/log/debug.log'
accesslog = '/var/log/access.log'
access_log_format = '%(h)s %(t)s %(U)s %(q)s'
errorlog = '/var/log/error.log'
# process name
proc_name = 'vservice'
pidfile = 'log/gunicorn.pid'
# set process daemon, not use in default
#daemon = True
# number of processes
workers = multiprocessing.cpu_count() * 2 + 1
# number of threads of per process
threads = multiprocessing.cpu_count() * 2
worker_class = 'gevent'
然后通過supervisor來啟動(dòng)我們的服務(wù),配置文件如下:
[program:demo]
command=/home/venv/bin/gunicorn -c /home/myproject/gun.py demo:app
directory=/home/myproject
startsecs=0
stopwaitsecs=0
autostart=false
autorestart=false
stdout_logfile=/home/myproject/log/gunicorn.log
stderr_logfile=/home/myproject/log/gunicorn.err
其中demo指的就是你的網(wǎng)站的程序普筹,而app指的是文件中的實(shí)例败明。
通過supervisor -c supervisor.conf來啟動(dòng)程序就可以了。
參考鏈接:http://www.cnblogs.com/luis-allan/archive/2016/11/04/6029820.html