可以从多个线程安全地使用MySQLdb Connection和Cursor对象吗? [英] Can MySQLdb Connection and Cursor objects be safely used from with multiple threads?

查看:155
本文介绍了可以从多个线程安全地使用MySQLdb Connection和Cursor对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python 3.5中使用mysqlclient v1.3.10.连接到数据库后,您将获得一个连接对象,从该对象中将获得一个用于运行查询的游标对象.

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.

这些对象线程安全吗(即,我可以创建一组对象,然后在多个不同的python线程之间共享和使用它们)吗?

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

推荐答案

看来您做不到. MySQLdb文档状态(向下滚动至 threadsafety >):

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

通常的结果是:不要共享彼此之间的联系 线程.确实不值得您付出我的努力,最后, 这可能会影响性能,因为MySQL服务器运行单独的 每个连接的线程.您当然可以做诸如高速缓存之类的事情 池中的连接,并将这些连接分配给一个线程 时间.如果让两个线程同时使用连接,则 MySQL客户端库可能会崩溃.你去过 警告.

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.

这篇关于可以从多个线程安全地使用MySQLdb Connection和Cursor对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆