title: pymysql.err.InternalError: Packet sequence number wrong
body: <p>I am using python 2.7 and the pymysql module to extract data from a MySQL database. I keep getting the error (at the bottom) when I execute the following code to select data (the error follows execution of the query):</p>
  
  <pre><code>conn = pms.connect(Host,User,Pwd,DB)
  with conn:
      cur = conn.cursor()  # Create cursor to interact with MySQL
      cur.execute(query)
  </code></pre>
  
  <p>where the query is:</p>
  
  <pre><code>SELECT  Time,var1,var2,var3 FROM tableName WHERE Time BETWEEN '2017-02-08 00:00:00' AND '2017-02-15 00:00:00'
  </code></pre>
  
  <p>I am happy to post more of the code but it's very large so better to try and narrow down what might be causing the problem first I think.</p>
  
  <p>This problem only occurs when I try to extract large(ish) amounts of data (tens of millions of rows). In this case I am extracting 4 variables, including Time, from 100 Hz sample rate data. The primary key in the database table is the Time.</p>
  
  <p><strong>FYI</strong>: I am able to perform a similar request to MySQL using the COUNT command on the same time period with no error:</p>
  
  <pre><code>SELECT COUNT(Time) FROM tableName WHERE Time BETWEEN '2017-02-08 00:00:00' AND '2017-02-15 00:00:00'
  </code></pre>
  
  <p><strong>Theory 1:</strong> Judging from google results of this problem, I suspect that it may be a threading related issue arising from my use of the GUI. But I am not consciously creating any threads within this program. Having said that, I am learning as I go and may be naive to it.</p>
  
  <p><strong>Theory 2:</strong> Another suspicion is that when the file size exceeds a threshold limit, something in the way the data is read from MySQL changes. Perhaps new threads/connections are created to deal with the request and an error occurs because of limitations associated with pymysql's Threadsafety value of 1 (according to googling the problem).</p>
  
  <p><strong>Question:</strong> Could the connection time out after say 15 minutes?</p>
  
  <p><strong>Key points:</strong></p>
  
  <ul>
  <li>The MySQL query works fine if I type it into MySQL command prompt</li>
  <li>The code works fine for small datasets (~1 or 2 million rows)</li>
  <li>The error occurs when I start to extract larger data sets (tens of millions of rows or more)</li>
  <li>The error does not occur immediately, the line will run and the error occurs after maybe 15 minutes</li>
  <li>I am able to perform a similar request to mysql like a COUNT with large datasets with no error</li>
  <li>The code forms part of a tkinter GUI</li>
  <li>I open and close connections to the database a few times throughout the code, always using a 'with conn' statement so it is closed when I am done.</li>
  <li>I get the same error when extracting large amounts of data from other tables, with only 2 variables, or with up to 150 variables.</li>
  </ul>
  
  <p><strong>Error information:</strong> Below is the full error for your information. Thank you for taking the time to read this.</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2017.1\helpers\pydev\pydevd.py", line 1578, in &lt;module&gt;
      globals = debugger.run(setup['file'], None, None, is_module)
    File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2017.1\helpers\pydev\pydevd.py", line 1015, in run
      pydev_imports.execfile(file, globals, locals)  # execute the script
    File "M:/HKerr/python/DBread/DBread_27_v2/DBread_27.py", line 108, in &lt;module&gt;
      TimeB.strftime("%Y-%m-%d %H:%M:%S"),Columns,Host)
    File "M:/HKerr/python/DBread/DBread_27_v2\DBread_mdl_27.py", line 222, in MySQLread
      count+=1
    File "C:\Python27\lib\site-packages\pymysql\connections.py", line 842, in __exit__
      self.rollback()
    File "C:\Python27\lib\site-packages\pymysql\connections.py", line 793, in rollback
      self._read_ok_packet()
    File "C:\Python27\lib\site-packages\pymysql\connections.py", line 767, in _read_ok_packet
      pkt = self._read_packet()
    File "C:\Python27\lib\site-packages\pymysql\connections.py", line 1001, in _read_packet
      % (packet_number, self._next_seq_id))
  pymysql.err.InternalError: Packet sequence number wrong - got 185 expected 1
  </code></pre>