python部署方式_python常用的部署方式

包括cherrypy, tornado, gevent, meinheld, bjoern, gunicorn+gevent, gunicorn+meinheld这几种.

测试程序是flask写的

我这里直接上代码了.

from tornado.wsgi import WSGIContainer

from tornado.httpserver import HTTPServer

from tornado.ioloop import IOLoop

from flask_server import app, port

import bjoern

import cherrypy

from cherrypy import _cpwsgi_server

from meinheld import server

from gevent.wsgi import WSGIServer

# tornado方式部署,异步模式.

# http_server = HTTPServer(WSGIContainer(app))

# http_server.bind(port)

# http_server.start(0)

# # http_server.start(1)

# print("server running on {} port ...".format(port))

# IOLoop.current().start()

# bjoern部署方式,c语言,异步模式.注意,这种方式不支持ssl,也不能自定义headers

# print("bjoern running on {} port ..".format(port))

# bjoern.listen(app, "0.0.0.0", port)

# bjoern.run()

# cherrypy部署方式,py语言,线程池模式.生产环境使用较多

# cherrypy.tree.graft(app, "/")

# server = cherrypy._cpserver.Server()

# server.socket_host = '0.0.0.0'

# server.socket_port = port

# server.thread_pool = 1500

# server.thread_pool_max = 5000

# server.start()

# meinheld 方式,部分c的py.

# from meinheld import patch

# patch.patch_all()

# server.listen(("0.0.0.0", 5000))

# server.run(app)

# gevent 部署方式

server = WSGIServer(('0.0.0.0', port), app)

server.serve_forever()

可能大家注意到,里面没有gunicorn+gevent, gunicorn+meinheld这两种方式,那是因为这两种方式配置比较麻烦,我用了一个配置文件.gunicorn_config.py

# -*- coding: utf-8 -*-

import multiprocessing

"""gunicorn的配置文件"""

# 预加载资源

preload_app = True

# 绑定

bind = "0.0.0.0:5000"

# 进程数

workers = multiprocessing.cpu_count() * 2 + 1

# 线程数

threads = multiprocessing.cpu_count() * 2

# 等待队列最大长度,超过这个长度的链接将被拒绝连接

backlog = 2048

# 工作模式

# worker_class = "egg:meinheld#gunicorn_worker"

worker_class = "gevent"

# 最大客户客户端并发数量,对使用线程和协程的worker的工作有影响

worker_connections = 1200

# 进程名称

proc_name = 'gunicorn.pid'

# 进程pid记录文件

pidfile = 'app_run.log'

# 日志等级

loglevel = 'debug'

# 日志文件名

logfile = 'debug.log'

# 访问记录

accesslog = 'access.log'

# 访问记录格式

access_log_format = '%(h)s %(t)s %(U)s %(q)s'

"""

运行方式

命令行 gunicorn -c gunicorn_config.py flask_server:app

如果在虚拟环境下运行(双py的ubuntu之流需要虚拟环境,否则会路径混乱):

"""

测试的机器是一般的机器,1200客户端并发,使用异步的方式持续性发送请求.请求是需要读数据库的耗时操作.大致的性能对比如下:

组合

成功率

总耗时

备注

cherrypy

48%

18s

单进程

tornado

76%

9.5s

单进程

tornado

84%

4.5s

4进程

gevent

84%

6s

单进程

meinheld

84%

3.7s

单进程

bjoern

84%

3.7s

单进程

gunicorn+gevent

84%

4.3s

9进程

gunicorn+meinheld

84%

3.6s

9进程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值