FastPypython 的 http服務器+web框架
高性能python http服務器+web框架
源代碼只有600多行
性能比較如下
tornado 4kqps 多進程1wqps
nginx+tornado 9kqps
nginx+uwsgi 8kqps
django web.py均在8k以下
本server 3.2w qps
沒用任何python加速
不相信的可以自己壓測下哦
已經(jīng)真實使用到自己的多個項目之中,效果明顯
有需要優(yōu)化的地方或者建議歡迎聯(lián)系 qq 512284622
fastpy 是高性能 Python HTTP 服務器。
用戶文檔:
1、啟動:
指定監(jiān)聽端口即可啟動
python fastpy.py 8992
2、快速編寫cgi,支持運行時修改,無需重啟server
在fastpy.py同一目錄下
隨便建一個python 文件
例如:
example.py:
#-*- coding:utf-8 -*-
import sys
#定義一個同名example類
#定義一個tt函數(shù):
reload(sys)
sys.setdefaultencoding('utf8')
FastpyAutoUpdate=True
class example():
def tt(self, request, response_head):
#print request.form
#print request.getdic
#fileitem = request.filedic["upload_file"]
#fileitem.filename
#fileitem.file.read()
return "ccb"+request.path
則訪問該函數(shù)的url為 http://ip:port/example.tt
修改后保存,即可訪問,無需重啟
FastpyAutoUpdate 屬性可控制需不需要熱部署
FastpyAutoUpdate=true 支持熱部署,修改無需重啟
FastpyAutoUpdate=false 則不支持熱部署,修改需要重啟
tt函數(shù)必須帶兩個參數(shù)
request:表示請求的數(shù)據(jù) 默認帶以下屬性
headers: 頭部 (字典)
form: post參數(shù),包括form表單 (字典)
getdic: url參數(shù) (字典)
filedic: form表單中文件 (字典)
rfile: 原始http content內(nèi)容 (字符串)
action: python文件名 (這里為example)
method: 函數(shù)方法 (這里為tt)
command: (get or post)
path: url (字符串)
http_version: http版本號 (http 1.1)
response_head: 表示response內(nèi)容的頭部
例如如果要返回用gzip壓縮
則增加頭部
response_head["Content-Encoding"] = "gzip"
3、下載文件
默認靜態(tài)文件(包括html,js、css、圖片、文件等)放在static文件夾下
html和js、css會自動壓縮加速
例如把a.jpg放到與fastpy.py同一層目錄的static文件夾下
訪問的url為 http://ip:port/static/a.jpg
支持etag 客戶端緩存功能
(server 使用sendfile進行文件發(fā)送,不占內(nèi)存且快速)
4、支持網(wǎng)頁模板編寫
創(chuàng)建一個模板 template.html
<HTML>
<HEAD><TITLE>$title</TITLE></HEAD>
<BODY>
$contents
</BODY>
</HTML>
則對應的函數(shù):
def template(request,response_head):
t = Template(file="template.html")
t.title = "my title"
t.contents = "my contents"
return str(t)
模板實現(xiàn)使用了python最快速Cheetah開源模板,
性能約為webpy django thinkphp等模板的10倍以上:
http://my.oschina.net/whp/blog/112296
5、支持http/https透明代理
python proxy.py 8995
啟動后再瀏覽器配置代理即可使用,可以彌補nginx 不支持https代理的不足
6、設(shè)計模式:
每個線程一個實例:
fastpy是多進程內(nèi)再多線程的模式,每個線程一個example 類對象.
每個進程一個實例:
如果想讓某個對象或變量一個進程只定義一個,
可以使用單例模式:
class example(Singleton):
def __new__(cls, *args, **kw):
if not hasattr(cls, '_instance'):
orig = super(example,cls)
cls._instance = orig.__new__(cls, *args, **kw)
# do something when create
return cls._instance
所有進程一個實例:
因為fastpy是多進程的,如果想讓所有進程所有線程也只使用一個對象
可以直接使用python的多進程接口
mgr = multiprocessing.Manager()
ip_dic = mgr.dict()
這樣每個進程的每個線程里都共用這個ip_dic變量
