丘梓颖 发表于 2016-6-3 11:17:13

FastPy3.0 发布,高性能 python 协程web框架

欢迎加入运维网交流群:263444886  
  FastPy3.0 发布了,FastPy是python领域一个高性能的web框架,底层封装gevent协程模型,使得python原生库操作mysql或者http时自动变成异步模式,使用上又具有django、webpy的易用性特点。
  本次版本更新增加了gevent协程模式下数据库mysql连接池的使用例子,结合pymysql+gevent的特点,可以使得原本同步的mysql操作自动变为异步模式。对django、webpy等web开发读者有一定的参考价值 。
  用户文档:
  
  1、启动:
  指定监听端口即可启动
  python fastpy.py 8992
  
  2、快速编写cgi,支持运行时修改,无需重启server
  
  在fastpy.py同一目录下
  随便建一个python 文件
  例如:
  example.py:
   #-*- coding:utf-8 -*-  
   import sys
  
   #定义一个同名example类
  
   #定义一个tt函数:
  
   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
  则访问该函数的url为 http://ip:port/example.tt
  修改后保存,即可访问,无需重启
  FastpyAutoUpdate 属性可控制需不需要热部署
  FastpyAutoUpdate=true 支持热部署,修改无需重启
  FastpyAutoUpdate=false 则不支持热部署,修改需要重启
  tt函数必须带两个参数
  request:表示请求的数据 默认带以下属性
  headers: 头部 (字典)
  form:  post参数,包括form表单 (字典)
  getdic: url参数 (字典)
  filedic: form表单中文件 (字典)
  rfile: 原始http content内容  (字符串)
  action: python文件名 (这里为example)
  method: 函数方法    (这里为tt)
  command:  (get or post)
  path: url (字符串)
  http_version: http版本号 (http 1.1)
  response_head: 表示response内容的头部
  例如如果要返回用gzip压缩
  则增加头部
  response_head["Content-Encoding"] = "gzip"
  
  除了直接return作为response内容外
  fastpy还支持 request.ret(res) 的方式返回内容,以支持异步调用
  例如:
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))
  新起一个线程去读文件,然后原来的处理线程就可以继续去处理别的请求了,
  等到文件读完调用request.ret(res)返回给客户端
  
  在WithGevent目录里 我们提供了协程方式,
  将同步的调用变成异步(为什么这样读者可以查询gevent spawn原理,
  里面提供协程下转发http和数据库连接池的例子)
  
  3、支持超大文件上传下载
  默认静态文件(包括html,js、css、图片、文件等)放在static文件夹下
  html和js、css会自动压缩加速
  例如把a.jpg放到static文件夹下
  访问的url为 http://ip:port/static/a.jpg
  支持etag 客户端缓存功能
  (server 使用sendfile进行文件发送,不占内存且快速)
  
  4、支持网页模板编写
  创建一个模板 template.html
     
       $title
  
       
  
           $contents
  
       
  
   
  则对应的函数:
   def template(request,response_head):  
       t = Template(file="template.html")
  
       t.title  = "my title"
  
       t.contents  = "my contents"
  
       return str(t)
  模板实现使用了python最快速Cheetah开源模板,
  性能约为webpy django thinkphp等模板的10倍以上:
  http://my.oschina.net/whp/blog/112296
  
  5、支持http/https透明代理
  python proxy.py 8995
  启动后再浏览器配置代理即可使用,可以弥补nginx 不支持https代理的不足
页: [1]
查看完整版本: FastPy3.0 发布,高性能 python 协程web框架