Skip to content

Commit 76543b5

Browse files
Merge pull request #1 from JNevrly/msgpack_numpy_integration
Integrated msgpack-numpy for passing numpy arrays as arguments and re…
2 parents 660891e + 7738a12 commit 76543b5

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
requirements = [
3939
'msgpack>=0.5.2',
4040
'pyzmq>=13.1.0',
41+
'msgpack-numpy>=0.4.3',
4142
'future',
4243
]
4344

tests/test_events.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,30 @@ def test_msgpack():
197197
assert isinstance(event.header[u'message_id'], bytes)
198198
assert isinstance(event.header[u'v'], int)
199199
assert isinstance(event.args[0], str)
200+
201+
202+
def test_msgpack_numpy():
203+
import numpy as np
204+
context = zerorpc.Context()
205+
test_array = np.arange(10)
206+
event = zerorpc.Event(u'myevent', (test_array,), context=context)
207+
print(event)
208+
# note here that str is an unicode string in all Python version (thanks to
209+
# the builtin str import).
210+
assert isinstance(event.name, str)
211+
for key in event.header.keys():
212+
assert isinstance(key, str)
213+
assert isinstance(event.header[u'message_id'], bytes)
214+
assert isinstance(event.header[u'v'], int)
215+
assert isinstance(event.args[0], np.ndarray)
216+
217+
packed = event.pack()
218+
event = event.unpack(packed)
219+
print(event)
220+
assert isinstance(event.name, str)
221+
for key in event.header.keys():
222+
assert isinstance(key, str)
223+
assert isinstance(event.header[u'message_id'], bytes)
224+
assert isinstance(event.header[u'v'], int)
225+
assert isinstance(event.args[0], np.ndarray)
226+
assert np.array_equal(test_array, event.args[0])

zerorpc/events.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
from builtins import range
2929

3030
import msgpack
31+
import msgpack_numpy as m
32+
m.patch() # Monkeypatching msgpack to handle Numpy
33+
3134
import gevent.pool
3235
import gevent.queue
3336
import gevent.event

0 commit comments

Comments
 (0)