住房城市乡建设部网站,福永网站设计多少钱,wordpress 仪表盘 慢,腾讯风铃怎么建设网站我的程序是用python结合bottle框架写的#xff0c;但bottle自带wsgi原本只是单进程单线程运行模式(Bottle 默认运行在内置的 wsgiref 服务器上面。这个单线程的 HTTP 服务器在开发的时候特别有用#xff0c;但其性能低下#xff0c;在服务器负载不断增加的时候也许会是性能瓶…我的程序是用python结合bottle框架写的但bottle自带wsgi原本只是单进程单线程运行模式(Bottle 默认运行在内置的 wsgiref 服务器上面。这个单线程的 HTTP 服务器在开发的时候特别有用但其性能低下在服务器负载不断增加的时候也许会是性能瓶颈 一次只能响应一个请求)。为了提升程序的处理能力首先要启用多线程即在程序中使用gevent( 大多数服务器的线程池都限制了线程池中线程的数量避免创建和切换线程的代价。尽管和进程 (fork)比起来线程还是挺便宜的。但是也没便宜到可以接受为每一个请求创建一个线程。gevent 模块添加了 greenlet 的支持。 greenlet 和传统的线程类似但其创建只需消耗很少的资源。基于 gevent 的服务器可以生成成千上万的 greenlet为每个连接分配一个 greenlet 也毫无压力。阻塞greenlet也不会影响到服务器接受新的请求。同时处理的连接数理论上是没有限制的。)。只需要在run中加上 server‘gevent‘如下1 importgevent2 from gevent importmonkey.patch_all()3 代码段……4 run(host‘0.0.0.0‘, port8080, server‘gevent‘)尽管使用了多线程模式但这些线程都是跑在一个进程里所以需要开启多个进程来进一步提升并发处理能力因此在启用脚本的时候在run(port)里端口号不能写死应该使用变量来传递如下代码(在脚本执行时在需带一个参数这个参数是大于1024的整数否则报错停止脚本)importgevent,sysfrom gevent importmonkey.patch_all()#获取端口号try:portnum int(sys.argv[1])exceptException,e:print 请带上整数类型的端口号启动此程序logging.error(请带上整数类型的端口号启动此程序)sys.exit(1)if portnum 1024:print 端口号请大于1024logging.error(端口号请大于1024)sys.exit(1)代码段……run(host‘0.0.0.0‘, portportnum , server‘gevent‘)执行方式如下(osyw.py是我python程序名)python osyw.py 1124如果纯靠手动操作这些在生产上很不方便所以我写了个shell来管理这个shell定义了多个端口号然后去循环启用或停止python进程脚本大概如下(是用httpd改写的)#!/bin/bash## osyw Startup script for the osyw HTTP Server## chkconfig: - 88 18# description: osyw# processname: osyw# config:# config: /home/bottle/osyw/# pidfile: /var/run/osyw.pid#### BEGIN INIT INFO# Provides: osyw# Short-Description: start and stop osyw HTTP Server# Description: The osyw HTTP Server is an extensible server# implementing the current HTTP standards.### END INIT INFO# Source function library.. /etc/rc.d/init.d/functions# Path to the apachectl script, server binary, and short-form for messages.port_list(8811 8812 8813) #设置了3个端口#pidfile‘/var/run/osyw.pid‘pro_path‘/var/www/osyw/osyw.py‘ #程序路径log_path‘/var/www/osyw/log/access.log‘ #访问日志路径RETVAL0start() {for i in ${port_list[*]}dop/usr/sbin/lsof -i :${i} |wc -lif [ ${p} -ge 2]thenaction osyw ${i} already exists ! /bin/falseelse/usr/bin/python ${pro_path} ${i} ${log_path}RETVAL$?if [ ${RETVAL} 0 ]thenaction osyw ${i} start ... /bin/trueelseaction osyw ${i} start ... /bin/falsefifidonereturn$RETVAL}stop() {for i in ${port_list[*]}dopidfile/var/run/osyw_${i}.pidif [ -f ${pidfile} ]thenpidcat ${pidfile}kill -9${pid}RETVAL$?if [ ${RETVAL} 0 ]thenaction osyw ${i} stop ... /bin/trueelseaction osyw ${i} stop ... /bin/falsefirm -f ${pidfile}elseaction osyw ${i} Has stopped ! /bin/falsefidone}# See how we were called.case $1 instart)start;;stop)stop;;status)status -p ${pidfile} ‘osyw‘RETVAL$?;;restart)stopsleep 2start;;condrestart|try-restart)if status -p ${pidfile} ‘osyw‘ /dev/null; thenstopstartfi;;force-reload|reload)reload;;*)echo $Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}RETVAL2esacexit $RETVAL效果图本人的代码是用svn管理的所以上传代码后SVN钩子会调用shell脚本来重启这些程序以下是SVN钩子代码export LANGen_US.UTF-8/usr/bin/svn update --username xxxx --password xxxxxxxx /var/bottle/bin/bash /etc/init.d/osyw restart当然为了结合shellpython程序里也要做一些处理如自动把程序转为后台守护进程然后把进程ID写入文件以下是关键的python代码#定义PID路径pid_path ‘/var/run/osyw_%s.pid‘ %portnumdefdaemonize():把本脚本转为守护进程try:pidos.fork()if pid0:sys.exit(0)exceptException,e:logging.error(e)sys.exit(1)os.chdir(‘/‘)os.umask(0)os.setsid()try:pidos.fork()if pid0:sys.exit(0)exceptException,e:logging.error(e)sys.exit(1)PID str(os.getpid())with open(pid_path,‘w‘) as f:f.write(PID)其它代码段……if __name__ ‘__main__‘:try:from oscore import setting #导入配置文件if setting.status ‘online‘: #如果配置中是线上的则程序转入后台运行daemonize()exceptException:passapp default_app()app SessionMiddleware(app, session_opts) #sessionMiddleware是session插件run(appapp,host‘0.0.0.0‘, portportnum,server‘gevent‘)最好用nginx代理来负载这些端口我nginx和python程序是安装在同一台服务器上的以上是nginx反向代理的部分代码upstream myweb {#ip_hash;server 192.168.1.240:8811 weight4 max_fails2 fail_timeout30s;server 192.168.1.240:8812 weight4 max_fails2 fail_timeout30s;server 192.168.1.240:8813 weight4 max_fails2 fail_timeout30s;}server {listen 80;server_name 192.168.1.240;location /{proxy_pass http://myweb;proxy_set_header Host $host;proxy_set_header X-Forwarded-For $remote_addr;proxy_cache_key $host$uri$is_args$args;}access_log off;}做完这些后当访问80端口时nginx就会平均轮洵分配到每个端口上去,实现了多进程多线程的运行模式更有效的提升了并发处理能力原文http://www.cnblogs.com/drfdai/p/4518121.html