pymysql连接线程安全吗? pymysql游标线程安全吗? [英] Is pymysql connection thread safe? Is pymysql cursor thread safe?

查看:348
本文介绍了pymysql连接线程安全吗? pymysql游标线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有队列数据结构,其中多个线程消耗项,每个线程将使用PyMySQL写入数据库,线程之间不需要其他同步.

I have queue data structure where multiple thread consume items, each thread is going to write to a database using PyMySQL, no other sync is need among threads.

是否可以在所有线程中自由使用来自相同pymysql连接的相同光标?

Is race free to use the same cursor coming from the same pymysql connection in all threads?

种族是否可以自由地使用来自同一连接的每个线程使用不同的光标?

Is race free to use different cursor per thread coming from the same connection?

(当然,在多个线程中使用多个连接也是可以的,因为这种情况下没有共享资源,因此我对这种情况没有兴趣)

(of course to use multiple connection in multiple threads is ok, because that case has no shared resource, I has no interest in this case)

推荐答案

感谢El Ruso指出了一个方向

Thanks to El Ruso, for point one direction

在了解了DBAPI2的规范后,我在pymysql源代码中找到了答案,该规范指示了如何根据实现来回答这个问题.对于PyMySQL,这意味着它对于连接或游标都不是线程安全的.

I found the answer in the pymysql source after understand the DBAPI2 has a specification indicating how to answer this question depending on the implementation. In case of PyMySQL it means it is not threadsafe for connections nor cursors.

https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/初始化 .py#L40

https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/init.py#L40 Line

PyMySQL是线程安全的= 1表示:线程可以共享模块,但不能共享连接.

PyMySQL is threadsafe = 1 means: Threads may share the module, but not connections.

(请阅读PEP-0249规范 http://legacy.python .org/dev/peps/pep-0249/#threadsafety )

(Read the PEP-0249 specification http://legacy.python.org/dev/peps/pep-0249/#threadsafety)

这篇关于pymysql连接线程安全吗? pymysql游标线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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