I'm trying to pack and send columnar data over a socket connection.
To speed it up, I thought about splitting the packing (struct.pack
) to multiple processes.
To avoid pickling both ways, I thought it might be better to have the packing processes send the data themselves, as it was said socket objects can be pickled starting Python 3.4.
This is a simplified version of what I have at work:
import socket
from multiprocessing import Pool
from struct import pack
# Start and connect a socket
s = socket.socket()
s.connect((ip, port))
# Data to be packed and sent in this order
data1 = 1, 2, 3, 4
data2 = 5, 6, 7, 8
data3 = 9, 10, 11, 12
# Top level column packer/sender for mp.pool
def send_column(column):
return s.send(pack(f'{len(column)}i', *column))
pool = Pool()
# Will this necessarily send the data in order?
pool.map(send_column, (data1, data2, data3))
My question is - Is it guaranteed the data will be sent in order?
If not, what's a prudent way to make sure it does?
I thought about a global counter for processes to check if it's their turn yet, but I'd be happy to hear better ideas.