Closed fatpo closed 6 years ago
0.2.9升级到0.3.6后,解决了这个问题。。 但是如果再从0.3.6降级到0.2.9后,报错:
with (yield chanzai_pool.Connection()) as conn:
File "/Users/ouyang/PycharmProjects/python_venv/myapp_venv/lib/python2.7/site-packages/tornado/gen.py", line 1055, in run
value = future.result()
File "/Users/ouyang/PycharmProjects/python_venv/myapp_venv/lib/python2.7/site-packages/tornado/concurrent.py", line 238, in result
raise_exc_info(self._exc_info)
File "/Users/ouyang/PycharmProjects/python_venv/myapp_venv/lib/python2.7/site-packages/tormysql/util.py", line 14, in finish
result = fun(*args, **kwargs)
File "/Users/ouyang/PycharmProjects/python_venv/myapp_venv/lib/python2.7/site-packages/tormysql/connections.py", line 268, in connect
raise exc
OperationalError: (2003, 'Can\'t connect to MySQL server on 127.0.0.1:3306 (OperationalError(2006, "MySQL server has gone away (StreamClosedError(\'Stream is closed\',))"))')
2018-02-01 19:26:54,121 140735576007488-MainThread-web.py-2063 [ERROR] 500 POST /api?type=json (127.0.0.1) 1260.81ms
发现是因为tornado被升级到4.5.2,降级之...发现bug竟然修复了... orz... 也就是说,我进行了一次升级
pip install tornado==4.5.2
pip install tormysql==0.3.6
再降级
pip install tornado==4.2
pip install tormysql==0.2.9
就修复了bug...
之所以会出现CursorNotReadAllDataError这个错误,是因为在python2中,with不支持异步语法,退出with的时候并不能等待连接完成所有数据读取,如果sql查询了超过1条数据,但你只读取第一条,此时退出cursor并继续进行下一次查询,那么就极有可能这些未读取的数据会出现在下一次的结果当中,导致数据混乱,所以with语法退出的时候会检查是否已经完全读取了返回数据,否则就会raise出CursorNotReadAllDataError这个异常,cursor使用fetchall读取所有数据之后就不会再出现这个问题了
Python3支持异步with语法,所以可以安全的使用异步语法保证连接完成数据读取过程
使用fetchone()只拿第一条数据,可能返回n条,此时with生命周期结束,就有可能造成结果集混乱??
岂不是太过于可怕。。
难道每次使用fetchone()我都要在sql语句中加一个limit 1?
fetchone通过连接返回数据是异步的,yield就是标记等待数据返回啊,但是with退出exit并不支持yield等待语法,不能完成等待数据读取完成的过程,所以只能外部完成数据读取,然后再退出with作用域 如果你使用Python2的话,那么要么限制limit1,要么使用fetchall,然后再取第一条数据 如果python3的话,with有异步语法支持,你直接只用异步语法就行
版本: tormysql==0.2.9 tornado==4.2
我的代码如下: 外层:
内层:
在我上洗手间之前,bug稳定重现,每一次都是执行到以下sql出错:
郁闷的我出去上了个洗手间,然后回来,把sql改成:
再也没出现bug。。。 为什么呢...不就是user_name和nick_name的顺序换了一下而已嘛??? orz..