I have a multiprocess application that processes large volumes of data in parallel and at the end loads CSV files into my DB.
The code starts off by creating n number of Python Processes. In each of these processes, I create a separate connection and cursor which I try to use to load this data however I keep getting an error with: pymysql.err.InternalError: Packet sequence number wrong - got 3 expected 2
Sample code here but obviously you would need to put in connection details etc:
from multiprocessing import Process
import pymysql
class RawFileExtractor(Process):
def __init__(self, file_location):
Process.__init__(self, daemon=True)
self.file_location = file_location
def run(self):
self.load_csv()
def load_csv(self):
mysql_connection = pymysql.connect(user=user, password=password, host=host, database=database,
max_allowed_packet=128000000, connect_timeout=100000, local_infile=True)
mysql_cursor = mysql_connection.cursor(pymysql.cursors.DictCursor)
mysql_cursor.execute("LOAD DATA LOCAL INFILE '{file_location}' INTO TABLE c210.table_name "
"FIELDS TERMINATED BY '\t' "
"LINES TERMINATED BY '\n';".format(file_location=self.file_location))
mysql_connection.commit()
mysql_connection.close()
processes = []
file_locations = ["/location/one.csv", "/location/two.csv"]
for i, file_location in enumerate(file_locations):
processes[i] = RawFileExtractor(file_location=file_location)
processes[i].start()
Of course my actual script/processes do a lot more than just load the data but this should be sufficient to replicate the issue.
I also know that this isn't a problem from the actual DB as I can load files without any problem using the python MySQL connector tool but I don't want to use it as it's buggy.
Full error below:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/connections.py", line 986, in _write_bytes
self.socket.sendall(data)
BrokenPipeError: [Errno 32] Broken pipe
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/connections.py", line 1457, in send_data
conn.write_packet(chunk)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/connections.py", line 936, in write_packet
self._write_bytes(data)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/connections.py", line 988, in _write_bytes
raise err.OperationalError(2006, "MySQL server has gone away (%r)" % (e,))
pymysql.err.OperationalError: (2006, "MySQL server has gone away (BrokenPipeError(32, 'Broken pipe'))")
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/connections.py", line 986, in _write_bytes
self.socket.sendall(data)
BrokenPipeError: [Errno 32] Broken pipe
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/connections.py", line 1326, in _read_load_local_packet
sender.send_data()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/connections.py", line 1462, in send_data
conn.write_packet(b'')
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/connections.py", line 936, in write_packet
self._write_bytes(data)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/connections.py", line 988, in _write_bytes
raise err.OperationalError(2006, "MySQL server has gone away (%r)" % (e,))
pymysql.err.OperationalError: (2006, "MySQL server has gone away (BrokenPipeError(32, 'Broken pipe'))")
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/process.py", line 254, in _bootstrap
self.run()
File "/Users/maldeiri/raw_data_processing/raw_file_extractor.py", line 109, in run
"LINES TERMINATED BY '\n';".format(file_location=self.delta_stream_load_file_name))
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/cursors.py", line 146, in execute
result = self._query(query)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/cursors.py", line 296, in _query
conn.query(q)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/connections.py", line 819, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/connections.py", line 1001, in _read_query_result
result.read()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/connections.py", line 1290, in read
self._read_load_local_packet(first_packet)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/connections.py", line 1328, in _read_load_local_packet
self.connection._read_packet() # skip ok packet
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pymysql/connections.py", line 952, in _read_packet
(packet_number, self._next_seq_id))
pymysql.err.InternalError: Packet sequence number wrong - got 3 expected 2
If you need any further information please let me know.
Python version: 3.4
PyMYSQL version: 0.7.1
OS: MAC