Skip to content

Integrated msgpack-numpy for serializing numpy arrays #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
requirements = [
'msgpack>=0.5.2',
'pyzmq>=13.1.0',
'msgpack-numpy>=0.4.3',
'future',
]

Expand Down
27 changes: 27 additions & 0 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,30 @@ def test_msgpack():
assert isinstance(event.header[u'message_id'], bytes)
assert isinstance(event.header[u'v'], int)
assert isinstance(event.args[0], str)


def test_msgpack_numpy():
import numpy as np
context = zerorpc.Context()
test_array = np.arange(10)
event = zerorpc.Event(u'myevent', (test_array,), context=context)
print(event)
# note here that str is an unicode string in all Python version (thanks to
# the builtin str import).
assert isinstance(event.name, str)
for key in event.header.keys():
assert isinstance(key, str)
assert isinstance(event.header[u'message_id'], bytes)
assert isinstance(event.header[u'v'], int)
assert isinstance(event.args[0], np.ndarray)

packed = event.pack()
event = event.unpack(packed)
print(event)
assert isinstance(event.name, str)
for key in event.header.keys():
assert isinstance(key, str)
assert isinstance(event.header[u'message_id'], bytes)
assert isinstance(event.header[u'v'], int)
assert isinstance(event.args[0], np.ndarray)
assert np.array_equal(test_array, event.args[0])
3 changes: 3 additions & 0 deletions zerorpc/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
from builtins import range

import msgpack
import msgpack_numpy as m
m.patch() # Monkeypatching msgpack to handle Numpy

import gevent.pool
import gevent.queue
import gevent.event
Expand Down