TorMySQL help.connection with 封装
from tornado.ioloop import IOLoop
from tornado import gen
from contextlib import contextmanager
import tormysql
class MyConnectionPool(tormysql.helpers.ConnectionPool):
@contextmanager
@gen.coroutine
def insert(self):
try:
tx = yield pool.begin()
yield tx
except:
yield tx.rollback()
raise
else:
yield tx.commit()
pool = MyConnectionPool(
max_connections = 20,
idle_seconds = 7200,
wait_connection_timeout = 3,
host = "127.0.0.1",
user = "root",
passwd = "passwd",
db = "test",
charset = "utf8"
)
@gen.coroutine
def test():
with pool.insert() as tx:
tx.execute("INSERT INTO test(id) VALUES(1)")
yield pool.close()
ioloop = IOLoop.instance()
ioloop.run_sync(test)