pymysql.err.InternalError: Packet sequence number wrong - got 4 expected 1

在django里面使用pymysql的方式进行链接, 结果就悲剧了.
 
PyMySQL is not thread safty to share connections as we did (we shared the class instance between multiple files as a global instance - in the class there is only one connection), it is labled as 1:

threadsafety = 1

According to PEP 249:

1 - Threads may share the module, but not connections.

One of the comments in PyMySQL github issue:

you need one pysql.connect() for each process/thread. As far as I know that's the only way to fix it. PyMySQL is not thread safe, so the same connection can't be used across multiple threads.

Any way if you were thinking of using other python package called MySQLdb for your threading application, notice to MySQLdb message:

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. For threaded applications, try using a connection pool. This can be done using the Pool module.

Eventually we managed to use Django ORM and we are writing only for our specific table, managed by using inspectdb.

0 个评论

要回复文章请先登录注册