Is pymysql connection thread safe? Is pymysql curs

2020-07-24 03:56发布

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条回答
霸刀☆藐视天下
2楼-- · 2020-07-24 04:20

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)

查看更多
登录 后发表回答