MySQLdb Connection 和 Cursor 对象可以安全地用于多线程吗?
Can MySQLdb Connection and Cursor objects be safely used from with multiple threads?
我在 Python 3.5 中使用 mysqlclient v1.3.10。连接到数据库后,您会得到一个连接对象,从中您会得到一个用于 运行 查询的游标对象。
这些对象线程安全吗(即我可以创建一组然后在多个不同的 python 线程之间共享和使用它们)吗?
看来你不能。 MySQLdb documentation 状态(向下滚动到 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.
我在 Python 3.5 中使用 mysqlclient v1.3.10。连接到数据库后,您会得到一个连接对象,从中您会得到一个用于 运行 查询的游标对象。
这些对象线程安全吗(即我可以创建一组然后在多个不同的 python 线程之间共享和使用它们)吗?
看来你不能。 MySQLdb documentation 状态(向下滚动到 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.