Is pymysql connection thread safe? Is pymysql cursor thread safe?

戏子无情 提交于 2020-01-13 06:18:17

问题


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.

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)


回答1:


Thanks to El Ruso, for point one direction

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/init.py#L40 Line

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

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



来源:https://stackoverflow.com/questions/47163438/is-pymysql-connection-thread-safe-is-pymysql-cursor-thread-safe

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