Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on May 9, 2023. It is now read-only.
/greentorPublic archive

Patch pymysql to support tornado asynchronous

License

NotificationsYou must be signed in to change notification settings

zhu327/greentor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

greentor is a fork ofgTornado

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-8fromgreentorimportgreengreen.enable_debug()fromgreentorimportmysqlmysql.patch_pymysql()
  1. green.enable_debug()非必须,开启greenlet调试模式,可以打印greenlet switch过程
  2. mysql.patch_pymysql()给pymysql打异步补丁,异步的pymysql依赖于Tornado,在Tornado的IOLoop start后才能正常使用

RequestHandler中使用

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

fromgreentorimportgreenfromgreentorimportmysqlmysql.patch_pymysql()importtornado.webclassMainHandler(tornado.web.RequestHandler):@green.greendefget(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的方式

fromgreentorimportgreenfromgreentorimportmysqlmysql.patch_pymysql()importtornado.webimporttornado.gen@green.greendeftest_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()returnresultclassMainHandler(tornado.web.RequestHandler):@tornado.gen.coroutinedefget(self):result=yieldtest_mysql()self.finish(result[2])

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

fromgreentorimportgreenfromgreentorimportmysqlmysql.patch_pymysql()importtornado.webimporttornado.gendeftest_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()returnresultclassMainHandler(tornado.web.RequestHandler):@tornado.gen.coroutinedefget(self):result=yieldgreen.spawn(test_mysql)self.finish(result[2])

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

实例

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

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

About

Patch pymysql to support tornado asynchronous

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp