Python论坛  - 讨论区

标题:[python-chinese] [MySQLdb] 多线程操作问题

2007年03月08日 星期四 01:05

bird devdoer devdoer在gmail.com
星期四 三月 8 01:05:23 HKT 2007

多线程访问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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年03月08日 星期四 09:27

东子 hydonlee在gmail.com
星期四 三月 8 09:27:22 HKT 2007

你这个多线程操作是不是有问题的?
共用了同一个连接???

如果没有记错, 数据库是不能通过一个连接同时处理多个请求的.

在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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年03月08日 星期四 14:03

bird devdoer devdoer在gmail.com
星期四 三月 8 14:03:29 HKT 2007

是啊 ,多线程共享同一个连接,在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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号