Can MySQLdb Connection and Cursor objects be safely used from with multiple threads?

回眸只為那壹抹淺笑 提交于 2019-12-07 21:41:45

问题


I'm using mysqlclient v1.3.10 in Python 3.5. After connecting to the database, you get a connection object, from which you get a cursor object that you use to run your queries.

Are these objects thread safe (i.e. can I create one set and then share and use them between multiple different python threads)?


回答1:


It appears that you can't. The MySQLdb documentation states (scroll down a little further to threadsafety):

The general upshot of this is: Don’t share connections between threads. It’s really not worth your effort or mine, and in the end, will probably hurt performance, since the MySQL server runs a separate thread for each connection. You can certainly do things like cache connections in a pool, and give those connections to one thread at a time. If you let two threads use a connection simultaneously, the MySQL client library will probably upchuck and die. You have been warned.



来源:https://stackoverflow.com/questions/45636492/can-mysqldb-connection-and-cursor-objects-be-safely-used-from-with-multiple-thre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!