Tornado异步MySQL驱动程序

zoom-tormysql的Python项目详细描述


tormysql

Build Status

性能最高的异步mysql驱动程序。

PYPI页面:https://pypi.python.org/pypi/tormysql

关于

提供一个基于未来的api和greenlet,用于对 MySQL。

同时支持tornadoasyncio

安装

pip install TorMySQL

二手龙卷风

示例池

from tornado.ioloop import IOLoop
from tornado import gen
import tormysql

pool = tormysql.ConnectionPool(
    max_connections = 20, #max open connections
    idle_seconds = 7200, #conntion idle timeout time, 0 is not timeout
    wait_connection_timeout = 3, #wait connection timeout
    host = "127.0.0.1",
    user = "root",
    passwd = "TEST",
    db = "test",
    charset = "utf8"
)

@gen.coroutine
def test():
    with (yield pool.Connection()) as conn:
        try:
            with conn.cursor() as cursor:
                yield cursor.execute("INSERT INTO test(id) VALUES(1)")
        except:
            yield conn.rollback()
        else:
            yield conn.commit()

        with conn.cursor() as cursor:
            yield cursor.execute("SELECT * FROM test")
            datas = cursor.fetchall()

    print datas

    yield pool.close()

ioloop = IOLoop.instance()
ioloop.run_sync(test)

示例助手

from tornado.ioloop import IOLoop
from tornado import gen
import tormysql

pool = tormysql.helpers.ConnectionPool(
    max_connections = 20, #max open connections
    idle_seconds = 7200, #conntion idle timeout time, 0 is not timeout
    wait_connection_timeout = 3, #wait connection timeout
    host = "127.0.0.1",
    user = "root",
    passwd = "TEST",
    db = "test",
    charset = "utf8"
)

@gen.coroutine
def test():
    tx = yield pool.begin()
    try:
        yield tx.execute("INSERT INTO test(id) VALUES(1)")
    except:
        yield tx.rollback()
    else:
        yield tx.commit()

    cursor = yield pool.execute("SELECT * FROM test")
    datas = cursor.fetchall()

    print datas

    yield pool.close()

ioloop = IOLoop.instance()
ioloop.run_sync(test)

单独使用异步

示例池

from asyncio import events
import tormysql

pool = tormysql.ConnectionPool(
   max_connections = 20, #max open connections
   idle_seconds = 7200, #conntion idle timeout time, 0 is not timeout
   wait_connection_timeout = 3, #wait connection timeout
   host = "127.0.0.1",
   user = "root",
   passwd = "TEST",
   db = "test",
   charset = "utf8"
)

async def test():
   async with await pool.Connection() as conn:
       try:
           async with conn.cursor() as cursor:
               await cursor.execute("INSERT INTO test(id) VALUES(1)")
       except:
           await conn.rollback()
       else:
           await conn.commit()

       async with conn.cursor() as cursor:
           await cursor.execute("SELECT * FROM test")
           datas = cursor.fetchall()

   print(datas)

   await pool.close()

ioloop = events.get_event_loop()
ioloop.run_until_complete(test)

示例助手

from asyncio import events
import tormysql

pool = tormysql.helpers.ConnectionPool(
   max_connections = 20, #max open connections
   idle_seconds = 7200, #conntion idle timeout time, 0 is not timeout
   wait_connection_timeout = 3, #wait connection timeout
   host = "127.0.0.1",
   user = "root",
   passwd = "TEST",
   db = "test",
   charset = "utf8"
)

async def test():
   async with await pool.begin() as tx:
       await tx.execute("INSERT INTO test(id) VALUES(1)")

   cursor = await pool.execute("SELECT * FROM test")
   datas = cursor.fetchall()

   print(datas)

   await pool.close()

ioloop = events.get_event_loop()
ioloop.run_until_complete(test)

资源

你可以阅读PyMySQL Documentation 在线获取更多信息。

许可证

tormysql使用mit许可,详细信息请参见许可文件。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何使用“Wed,01 Jul 2015 17:32:41 EDT”解析字符串   java Storm apache升级(1.0.0到2.0.0)   java类驻留在不同的目录中,而不是包指定的目录。为什么?   将Java中的图像缩放到非常小的维度   java如何通过子文档从自定义方面访问ElasticSearch parentdoc字段   java如何在RationalSoftwareArchitect中使用findbugs?   Java中的事件提升处理   java值被添加到arrayList的所有索引中,而不是在“”时添加到最后一个索引中。正在使用arraylist的add()方法   JFrame中的java JPanel派生类   java如何用循环和异步方法模拟类   java Android阻止可绘制背景超出视图范围   为客户排序Java阵列   java Apache poi如何将工作表设置为枚举位置值属性?   java Rhino在使用自定义类参数调用javascript函数时出错   java格式化日期从年月日到年月日   spring如何修复java。lang.illegalargumentexception在此特定场景中是否尝试创建具有null实体的合并事件?   java如何创建更好的对象