PyMySQL: Packet sequence number wrong

I’ve been using PyMySQL 0.6.6 for a while now with no issues. However after updating to 0.7.1, I’m getting this error a lot, and PyMySQL has become completely unusable for me because of it.

  File "/usr/local/lib/python3.4/dist-packages/pymysql/cursors.py", line 146, in execute
    result = self._query(query)
  File "/usr/local/lib/python3.4/dist-packages/pymysql/cursors.py", line 296, in _query
    conn.query(q)
  File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 819, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 1001, in _read_query_result
    result.read()
  File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 1285, in read
    first_packet = self.connection._read_packet()
  File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 952, in _read_packet
    (packet_number, self._next_seq_id))
pymysql.err.InternalError: Packet sequence number wrong - got 101 expected 1

The packet numbers vary, but I’m getting this for every query now, no matter how simple. Things will work as expected for a few minutes after starting my script, but soon after these errors will begin appearing and no queries will work anymore.

I’m not sure if there’s something I can do to resolve this in my script (some changes made in newer versions that I need to accommodate for) or if I should just figure out how to install the older version as a temporary solution. As it is now, this is a complete showstopper for me.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (5 by maintainers)

Most upvoted comments

@methane

SQLAlchemy also has this problem in multithread if you use one session,like this:

# base.py
engine = create_engine(
    get_db_str(),
    echo=False,
    pool_recycle=300,
    poolclass=QueuePool,
    pool_size=10,
    max_overflow=10,
    pool_timeout=30,
    listeners=[Reconnect()]
)

Session = sessionmaker(bind=engine)

# user1.py
from .base import Session
session1 = Session()
session1.query

# user2.py
from .base import Session
session2 = Session()
session2.query

I use ab command send 1000 requests to system. System raise that exception after some requests.

Yes.

@kadnan Yes, 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.

@DougTy @ahmedsaalah I’v faced this problem before, then I created a module which provide connection pools in multi-threads mode. see here @methane my code maybe ugly, but I’ll be appreciate if you can make some suggestions.