今天在用 TorMySQL 的时候遇到了一个问题,就是无法忽略表不存在的异常:
问题描述
我的代码如下:
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/join", RegisterHandler),
(r"/", MainHandler),
(r"/websocket", EchoWebSocket),
]
settings = dict(
cookie_secret="__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE_",
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
xsrf_cookie=True,
debug=True,
schema=os.path.join(os.path.dirname(__file__), "schema.sql"),
)
self.pool = tormysql.ConnectionPool(
max_connections = 20,
idle_seconds = 7200,
wait_connection_timeout = 3,
host = "127.0.0.1",
user = "chat",
password = "chat",
db = "chat",
charset = "utf8"
)
super(Application, self).__init__(handlers, **settings)
self.may_create_db()
@gen.coroutine
def may_create_db(self):
with (yield self.pool.Connection()) as conn:
try:
with conn.cursor() as cursor:
yield cursor.execute("SELECT * FROM user")
except pymysql.err.ProgrammingError as e:
logging.error(e)
subprocess.Popen(['mysql',
'--host={}'.format('127.0.0.1'),
'--database={}'.format('chat'),
'--user={}'.format('chat'),
'--password={}'.format('chat')],
stdin=open(self.settings['schema']))
except:
yield conn.rollback()
else:
yield conn.commit()
yield self.pool.close()
在 may_create_db函数中,我会尝试去连接数据库,如果对应的表不存在的话,那么就会创建相关的表,但现在运行的时候遇到了一点问题,就是那个pymysql.err.ProgrammingError 还是会显示出来。
运行结果如下:
➜ /home/yundongx/gitroom/chat (dev) ✗ [chat]$ python app.py
2016-10-04 21:27:10,677 INFO: Server starts on port 8888
2016-10-04 21:27:10,704 ERROR: (1146, "Table 'chat.user' doesn't exist")
mysql: [Warning] Using a password on the command line interface can be insecure.
2016-10-04 21:27:10,710 ERROR: Future <tornado.concurrent.Future object at 0x7f39862ca2e8> exception was never retrieved: Traceback (most recent call last):
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/tornado/gen.py", line 1021, in run
yielded = self.gen.throw(*exc_info)
File "app.py", line 94, in may_create_db
yield cursor.execute("SELECT * FROM test")
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/tornado/gen.py", line 1015, in run
value = future.result()
File "/home/yundongx/.virtualenvs/chat/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/chat/lib/python3.5/site-packages/tormysql/util.py", line 14, in finish
result = fun(*args, **kwargs)
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/pymysql/cursors.py", line 166, in execute
result = self._query(query)
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/pymysql/cursors.py", line 322, in _query
conn.query(q)
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/pymysql/connections.py", line 835, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/pymysql/connections.py", line 1019, in _read_query_result
result.read()
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/pymysql/connections.py", line 1302, in read
first_packet = self.connection._read_packet()
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/pymysql/connections.py", line 981, in _read_packet
packet.check_error()
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/pymysql/connections.py", line 393, in check_error
err.raise_mysql_exception(self._data)
File "/home/yundongx/.virtualenvs/chat/lib/python3.5/site-packages/pymysql/err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1146, "Table 'chat.test' doesn't exist")
我的疑问
- 我调用
may_create_db的方式是否正确? - 我了解到那个异常显示的原因是因为相关Future的异常没有被取出,所以自动加到了logging里面,打印了出来,如何才能让那个异常不显示出来呢?