python协程框架_FastPy3.0 发布,高性能 python 协程web框架
FastPy3.0 發(fā)布了,FastPy是python領(lǐng)域一個(gè)高性能的web框架,底層封裝gevent協(xié)程模型,使得python原生庫(kù)操作mysql或者h(yuǎn)ttp時(shí)自動(dòng)變成異步模式,使用上又具有django、webpy的易用性特點(diǎn)。
本次版本更新增加了gevent協(xié)程模式下數(shù)據(jù)庫(kù)mysql連接池的使用例子,結(jié)合pymysql+gevent的特點(diǎn),可以使得原本同步的mysql操作自動(dòng)變?yōu)楫惒侥J?。?duì)django、webpy等web開(kāi)發(fā)讀者有一定的參考價(jià)值 。
用戶文檔:
1、啟動(dòng):
指定監(jiān)聽(tīng)端口即可啟動(dòng)
python fastpy.py 8992
2、快速編寫(xiě)cgi,支持運(yùn)行時(shí)修改,無(wú)需重啟server
在fastpy.py同一目錄下
隨便建一個(gè)python 文件
例如:
example.py:#-*-?coding:utf-8?-*-
import?sys
#定義一個(gè)同名example類
#定義一個(gè)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
則訪問(wèn)該函數(shù)的url為 http://ip:port/example.tt
修改后保存,即可訪問(wèn),無(wú)需重啟
FastpyAutoUpdate 屬性可控制需不需要熱部署
FastpyAutoUpdate=true 支持熱部署,修改無(wú)需重啟
FastpyAutoUpdate=false 則不支持熱部署,修改需要重啟
tt函數(shù)必須帶兩個(gè)參數(shù)
request:表示請(qǐng)求的數(shù)據(jù) 默認(rèn)帶以下屬性
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版本號(hào) (http 1.1)
response_head: 表示response內(nèi)容的頭部
例如如果要返回用gzip壓縮
則增加頭部
response_head["Content-Encoding"] = "gzip"
除了直接return作為response內(nèi)容外
fastpy還支持 request.ret(res) 的方式返回內(nèi)容,以支持異步調(diào)用
例如:def?readfile(request,?response_head):
content?=?open("a.txt").read()
request.ret(content)
class?example():
def?tt(self,?request,?response_head):
#do?some?thind?prepare
thread.start_new_thread(readfile,(request,response_head))
新起一個(gè)線程去讀文件,然后原來(lái)的處理線程就可以繼續(xù)去處理別的請(qǐng)求了,
等到文件讀完調(diào)用request.ret(res)返回給客戶端
在WithGevent目錄里 我們提供了協(xié)程方式,
將同步的調(diào)用變成異步(為什么這樣讀者可以查詢gevent spawn原理,
里面提供協(xié)程下轉(zhuǎn)發(fā)http和數(shù)據(jù)庫(kù)連接池的例子)
3、支持超大文件上傳下載
默認(rèn)靜態(tài)文件(包括html,js、css、圖片、文件等)放在static文件夾下
html和js、css會(huì)自動(dòng)壓縮加速
例如把a(bǔ).jpg放到static文件夾下
訪問(wèn)的url為 http://ip:port/static/a.jpg
支持etag 客戶端緩存功能
(server 使用sendfile進(jìn)行文件發(fā)送,不占內(nèi)存且快速)
4、支持網(wǎng)頁(yè)模板編寫(xiě)
創(chuàng)建一個(gè)模板 template.html
$title$contents
則對(duì)應(yīng)的函數(shù):def?template(request,response_head):
t?=?Template(file="template.html")
t.title??=?"my?title"
t.contents??=?"my?contents"
return?str(t)
模板實(shí)現(xiàn)使用了python最快速Cheetah開(kāi)源模板,
性能約為webpy django thinkphp等模板的10倍以上:
http://my.oschina.net/whp/blog/112296
5、支持http/https透明代理
python proxy.py 8995
啟動(dòng)后再瀏覽器配置代理即可使用,可以彌補(bǔ)nginx 不支持https代理的不足
總結(jié)
以上是生活随笔為你收集整理的python协程框架_FastPy3.0 发布,高性能 python 协程web框架的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 表达开心快乐的文案大全100个
- 下一篇: websocket python爬虫_p