2007年03月08日 星期四 16:57
bird devdoer,您好!
每个thread里做一个连接,而不是各个thread共用一个连接
在 2007-03-08 01:05:00 您写道:
>多线程访问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在gmail.com
>http://devdoer.blog.sohu.com/
>_______________________________________________
>python-chinese
>Post: send python-chinese在lists.python.cn
>Subscribe: send subscribe to python-chinese-request在lists.python.cn
>Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
>Detail Info: http://python.cn/mailman/listinfo/python-chinese
致
礼!
cry
zyqmail在tom.com
Zeuux © 2025
京ICP备05028076号