我这两天了解了一下 Python3 中的 async/await 语句,然后照猫画虎,对 TorMySQL 封装了一个 async with
语句的上下文管理器:
代码如下:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from tornado.ioloop import IOLoop
import tormysql
class get_cursor:
def __init__(self, pool):
""" get_cursor 上下文管理器
针对`async with`语句封装的上下文管理器
"""
self.pool = pool
async def __aenter__(self):
self.tx = await self.pool.begin()
return self.tx
async def __aexit__(self, exc_type, exc, tb):
if tb is None:
self.tx.commit()
else:
self.tx.rollback()
async def insert():
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 with get_cursor(pool) as cursor:
cursor.execute("INSERT INTO test(id) VALUES(1)")
ioloop = IOLoop.instance()
ioloop.run_sync(insert)
我执行了上述代码以后,抛出了如下的错误:
➜ /home/yundongx/tutorial/async [async]$ python3 async.py
ERROR:tornado.application:Future <tornado.concurrent.Future object at 0x7f1983708470> exception was never retrieved: Traceback (most recent call last):
File "/home/yundongx/.virtualenvs/async/lib/python3.5/site-packages/tornado/gen.py", line 1021, in run
yielded = self.gen.throw(*exc_info)
File "/home/yundongx/.virtualenvs/async/lib/python3.5/site-packages/tormysql/helpers.py", line 44, in commit
yield self._connection.commit()
File "/home/yundongx/.virtualenvs/async/lib/python3.5/site-packages/tornado/gen.py", line 1015, in run
value = future.result()
File "/home/yundongx/.virtualenvs/async/lib/python3.5/site-packages/tornado/concurrent.py", line 237, in result
raise_exc_info(self._exc_info)
File "<string>", line 3, in raise_exc_info
File "/home/yundongx/.virtualenvs/async/lib/python3.5/site-packages/tormysql/util.py", line 14, in finish
result = fun(*args, **kwargs)
File "/home/yundongx/.virtualenvs/async/lib/python3.5/site-packages/pymysql/connections.py", line 767, in commit
self._read_ok_packet()
File "/home/yundongx/.virtualenvs/async/lib/python3.5/site-packages/pymysql/connections.py", line 746, in _read_ok_packet
pkt = self._read_packet()
File "/home/yundongx/.virtualenvs/async/lib/python3.5/site-packages/pymysql/connections.py", line 961, in _read_packet
packet_header = self._read_bytes(4)
File "/home/yundongx/.virtualenvs/async/lib/python3.5/site-packages/tormysql/connections.py", line 308, in _read_bytes
future = self._rfile.read_bytes(num_bytes)
File "/home/yundongx/.virtualenvs/async/lib/python3.5/site-packages/tormysql/connections.py", line 97, in read
assert self._read_future is None, "Already reading"
AssertionError: Already reading
Exception ignored in: <generator object execute at 0x7f19836e2e60>
RuntimeError: generator ignored GeneratorExit
WARNING:root:Transaction has not committed or rollbacked <tormysql.connections.Connection object at 0x7f19837080b8> {'user': b'root', 'port': 3306, 'host': '127.0.0.1', 'database': b'test'}.
然后我去数据库中查询这个表,发现数据已经插入成功了。
请问一下这个异常是什么意思啊( PyMySQL 显示说Already reading
是什么意思啊)?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.