2007年03月08日 星期四 01:05
多线程访问mysql老出错,单线程没问题,代码如下: conn=MySQLdb.connect(user='david',passwd='gggggg',host='121.12.80.15') def t1(): key='123' while True: c=conn.cursor() r=c.execute('select let from test.test_table where key=%s',[key]) time.sleep(1) if __name__=='__main__': for i in xrange(10): thread=threading.Thread(target=t1) thread.start() 注:以上数据库地址为说明使用,假设存在。 报segment fault 错误如下: Exception in thread Thread-2: Traceback (most recent call last): File "threading.py", line 442, in __bootstrap self.run() File "threading.py", line 422, in run self.__target(*self.__args, **self.__kwargs) File "test_mysql.py", line 9, in t1 r=c.execute('select let from blog.bloginfo where fu=%s',[fu]) File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 137, in execute self.errorhandler(self, exc, value) File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler raise errorclass, errorvalue OperationalError: (1047, 'Unknown command') Exception in thread Thread-3: Traceback (most recent call last): File "threading.py", line 442, in __bootstrap self.run() File "threading.py", line 422, in run self.__target(*self.__args, **self.__kwargs) File "test_mysql.py", line 9, in t1 r=c.execute('select let from blog.bloginfo where fu=%s',[fu]) File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 137, in execute self.errorhandler(self, exc, value) File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler raise errorclass, errorvalue OperationalError: (2013, 'Lost connection to MySQL server during query') Segmentation fault -- devdoer devdoer at gmail.com http://devdoer.blog.sohu.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20070308/96f24ef0/attachment.html
2007年03月08日 星期四 09:27
你这个多线程操作是不是有问题的? 共用了同一个连接??? 如果没有记错, 数据库是不能通过一个连接同时处理多个请求的. 在07-3-8,bird devdoer <devdoer at gmail.com> 写道: > > 多线程访问mysql老出错,单线程没问题,代码如下: > > conn=MySQLdb.connect(user='david',passwd='gggggg',host='121.12.80.15') > def t1(): > key='123' > > while True: > c=conn.cursor() > r=c.execute('select let from test.test_table where > key=%s',[key]) > time.sleep(1) > > > > if __name__=='__main__': > for i in xrange(10): > thread=threading.Thread(target=t1) > thread.start() > 注:以上数据库地址为说明使用,假设存在。 > 报segment fault > 错误如下: > > Exception in thread Thread-2: > Traceback (most recent call last): > File "threading.py", line 442, in __bootstrap > self.run() > File "threading.py", line 422, in run > self.__target(*self.__args, **self.__kwargs) > File "test_mysql.py", line 9, in t1 > r=c.execute('select let from blog.bloginfo where fu=%s',[fu]) > File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 137, in > execute > self.errorhandler(self, exc, value) > File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 33, > in defaulterrorhandler > raise errorclass, errorvalue > OperationalError: (1047, 'Unknown command') > > Exception in thread Thread-3: > Traceback (most recent call last): > File "threading.py", line 442, in __bootstrap > self.run() > File "threading.py", line 422, in run > self.__target(*self.__args, **self.__kwargs) > File "test_mysql.py", line 9, in t1 > r=c.execute('select let from blog.bloginfo where fu=%s',[fu]) > File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 137, in > execute > self.errorhandler(self, exc, value) > File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 33, > in defaulterrorhandler > raise errorclass, errorvalue > OperationalError: (2013, 'Lost connection to MySQL server during query') > > Segmentation fault > > -- > devdoer > devdoer at gmail.com > http://devdoer.blog.sohu.com/ > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -- 只要人间真情在, 何必在意路泥泞 时风日下,和谐需要靠暴力达到!!! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20070308/4caa9561/attachment.html
2007年03月08日 星期四 14:03
是啊 ,多线程共享同一个连接,在execute前后加上锁保护就没问题了。 但是不明白mysqldb 客户端线程安全体现在哪里了?如果是一个线程一个连接,我觉得线程安全没有体现出啊 在07-3-8,东子 <hydonlee at gmail.com> 写道: > > 你这个多线程操作是不是有问题的? > 共用了同一个连接??? > > 如果没有记错, 数据库是不能通过一个连接同时处理多个请求的. > > 在07-3-8,bird devdoer <devdoer at gmail.com > 写道: > > > > 多线程访问mysql老出错,单线程没问题,代码如下: > > > > conn=MySQLdb.connect(user='david',passwd='gggggg',host='121.12.80.15') > > def t1(): > > key='123' > > > > while True: > > c=conn.cursor() > > r=c.execute('select let from test.test_table where > > key=%s',[key]) > > time.sleep(1) > > > > > > > > if __name__=='__main__': > > for i in xrange(10): > > thread=threading.Thread(target=t1) > > thread.start() > > 注:以上数据库地址为说明使用,假设存在。 > > 报segment fault > > 错误如下: > > > > Exception in thread Thread-2: > > Traceback (most recent call last): > > File "threading.py", line 442, in __bootstrap > > self.run() > > File "threading.py", line 422, in run > > self.__target(*self.__args, **self.__kwargs) > > File "test_mysql.py", line 9, in t1 > > r=c.execute('select let from blog.bloginfo where fu=%s',[fu]) > > File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 137, > > in execute > > self.errorhandler(self, exc, value) > > File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line > > 33, in defaulterrorhandler > > raise errorclass, errorvalue > > OperationalError: (1047, 'Unknown command') > > > > Exception in thread Thread-3: > > Traceback (most recent call last): > > File "threading.py", line 442, in __bootstrap > > self.run() > > File "threading.py", line 422, in run > > self.__target(*self.__args, **self.__kwargs) > > File "test_mysql.py", line 9, in t1 > > r=c.execute('select let from blog.bloginfo where fu=%s',[fu]) > > File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 137, > > in execute > > self.errorhandler(self, exc, value) > > File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line > > 33, in defaulterrorhandler > > raise errorclass, errorvalue > > OperationalError: (2013, 'Lost connection to MySQL server during query') > > > > > > Segmentation fault > > > > -- > > devdoer > > devdoer at gmail.com > > http://devdoer.blog.sohu.com/ > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to > > python-chinese-request at lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > -- > 只要人间真情在, 何必在意路泥泞 > 时风日下,和谐需要靠暴力达到!!! > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -- devdoer devdoer at gmail.com http://devdoer.blog.sohu.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20070308/b7ab9636/attachment.html
Zeuux © 2025
京ICP备05028076号