1

I need to send a pickled python dictionary containing several, large Pandas Dataframes over a websocket (Python API at both ends). To improve performance, I would like to compress the pickled object before sending it over the websocket. However, when doing this, the received message is always None.

Client:

df = pd.DataFrame({'a':[1,2,3,4]})
d = dict(b=df)
msg = zlib.compress(pickle.dumps(d),5)
socket.send(msg)

Server:

msg = socket.receive()
# msg is always None when called with client code above.
data = pickle.loads(zlib.decompress(msg))

Is there a better way to do this? I am using gevent-websocket through the Flask-Sockets framework.

1 Answer 1

3

To send binary data in Python 2.7, you must set binary=True in the client (ws4py):

socket.send(msg, Binary=True)

As another note, on the receiving side, you will need to wrap the msg in a buffer before sending to zlib.decompress()

msg = socket.receive()
data = pickle.loads(zlib.decompress(buffer(msg)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.