All Projects → zhu327 → greentor

zhu327 / greentor

Licence: MIT License
Patch pymysql to support tornado asynchronous

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to greentor

tornado-websocket-client-example
Websocket client application example built on top of Tornado.
Stars: ✭ 34 (+9.68%)
Mutual labels:  tornado
github-trending
GitHub trending API powered by Python Tornado.
Stars: ✭ 36 (+16.13%)
Mutual labels:  tornado
fastweb
fastweb is a web-server integration solution. It based on tornado, celery, thrift.
Stars: ✭ 17 (-45.16%)
Mutual labels:  tornado
django-hurricane
Hurricane is an initiative to fit Django perfectly with Kubernetes.
Stars: ✭ 53 (+70.97%)
Mutual labels:  tornado
gohook
【Souvenir】Python 使用 Tornado 框架实现 WebHook 自动部署 Git 项目。
Stars: ✭ 52 (+67.74%)
Mutual labels:  tornado
OpenScraper
An open source webapp for scraping: towards a public service for webscraping
Stars: ✭ 80 (+158.06%)
Mutual labels:  tornado
py-healthcheck
Write simple healthcheck functions for your Flask or Tornado apps.
Stars: ✭ 92 (+196.77%)
Mutual labels:  tornado
chat
Chat
Stars: ✭ 12 (-61.29%)
Mutual labels:  tornado
fast-poster
🔥🔥🔥 fastposter海报生成器,电商海报编辑器,电商海报设计器,fast快速生成海报 自定义海报制作 海报开发。二维码海报,图片海报,分享海报,二维码推广海报,支持Java Python PHP Go JS 小程序。基于Vue 和Pillow 演示地址:https://poster.prodapi.cn/
Stars: ✭ 329 (+961.29%)
Mutual labels:  tornado
Fukei
A socks proxy based Tornado
Stars: ✭ 82 (+164.52%)
Mutual labels:  tornado
tornado-aws
A low-level Amazon Web Services API client for Tornado
Stars: ✭ 12 (-61.29%)
Mutual labels:  tornado
nats.py2
A Tornado based Python 2 client for NATS
Stars: ✭ 62 (+100%)
Mutual labels:  tornado
MAVCesium
An experimental web based map display for MAVProxy based on Cesium
Stars: ✭ 28 (-9.68%)
Mutual labels:  tornado
apispec-webframeworks
Web framework plugins for apispec (formally in apispec.ext).
Stars: ✭ 25 (-19.35%)
Mutual labels:  tornado
memoize
Caching library for asynchronous Python applications.
Stars: ✭ 53 (+70.97%)
Mutual labels:  tornado
fixed-wing-sim
Matlab implementation to simulate the non-linear dynamics of a fixed-wing unmanned areal glider. Includes tools to calculate aerodynamic coefficients using a vortex lattice method implementation, and to extract longitudinal and lateral linear systems around the trimmed gliding state.
Stars: ✭ 72 (+132.26%)
Mutual labels:  tornado
factory
Docker microservice & Crawler by scrapy
Stars: ✭ 56 (+80.65%)
Mutual labels:  tornado
Face-Recognition-Class-Attendance-System
基于人脸识别的课堂考勤系统v2.0
Stars: ✭ 129 (+316.13%)
Mutual labels:  pymysql
webTimer
一款时间管理小工具的chrome插件
Stars: ✭ 18 (-41.94%)
Mutual labels:  tornado
facescore
人脸打分服务,通过tensorflow通过人脸识别,测年龄,最终返回打分的json
Stars: ✭ 31 (+0%)
Mutual labels:  tornado

greentor

greentor is a fork of gTornado

greentor通过给pymysql打补丁,使pymysql在Tornado中的运行过程变为异步IO,相比于其它支持Tornado的mysql驱动,greentor有以下不同

  1. 同步pymysql的写法
  2. 理论上可以支持各种ORM的调用异步

感谢@alex8224和他的gTornado

感谢@snower,参考他的TorMySQL优化了IOStream的读写性能

安装

pip install git+https://github.com/zhu327/greentor.git

使用

# coding: utf-8

from greentor import green
green.enable_debug()
from greentor import mysql
mysql.patch_pymysql()
  1. green.enable_debug() 非必须,开启greenlet调试模式,可以打印greenlet switch过程
  2. mysql.patch_pymysql() 给pymysql打异步补丁,异步的pymysql依赖于Tornado,在Tornado的IOLoop start后才能正常使用

RequestHandler中使用

涉及到pymysql的调用都需要运行在greenlet中,提供了3种方式实现同步代码转异步

from greentor import green
from greentor import mysql
mysql.patch_pymysql()
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    @green.green
    def get(self):
        connect = MySQLdb.connect(user='root',
                                  passwd='',
                                  db='test',
                                  host='localhost',
                                  port=3306)
        cursor = connect.cursor()
        cursor.execute('SELECT * FROM app_blog LIMIT 1')
        result = cursor.fetchone()
        cursor.close()
        connect.close()
        self.finish(result[2])

通过green.green装饰器使整个get方法都运行在greenlet中,这样是最方便的使用pymysql的方式

from greentor import green
from greentor import mysql
mysql.patch_pymysql()
import tornado.web
import tornado.gen

@green.green
def test_mysql():
    connect = MySQLdb.connect(user='root',
                              passwd='',
                              db='test',
                              host='localhost',
                              port=3306)
    cursor = connect.cursor()
    cursor.execute('SELECT * FROM app_blog LIMIT 1')
    result = cursor.fetchone()
    cursor.close()
    connect.close()
    return result


class MainHandler(tornado.web.RequestHandler):
    @tornado.gen.coroutine
    def get(self):
        result = yield test_mysql()
        self.finish(result[2])

通过green.green装饰器包装的函数会返回Future对象,可以在Tornado的协程中使用

from greentor import green
from greentor import mysql
mysql.patch_pymysql()
import tornado.web
import tornado.gen

def test_mysql():
    connect = MySQLdb.connect(user='root',
                              passwd='',
                              db='test',
                              host='localhost',
                              port=3306)
    cursor = connect.cursor()
    cursor.execute('SELECT * FROM app_blog LIMIT 1')
    result = cursor.fetchone()
    cursor.close()
    connect.close()
    return result


class MainHandler(tornado.web.RequestHandler):
    @tornado.gen.coroutine
    def get(self):
        result = yield green.spawn(test_mysql)
        self.finish(result[2])

green.spawn(callable_obj, *arg, **kwargs)的调用与green.green一致

实例

在tests目录下有一个使用纯pymysql调用的实例

demo目录下有一个完整的 Tornado + Django ORM 的环境,具体可以查看demo目录下的README

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].