Beispiel #1
0
    def test_buffer_numpy(self):
        """test non-copying numpy array messages"""
        try:
            import numpy
            from numpy.testing import assert_array_equal
        except ImportError:
            raise SkipTest("requires numpy")
        rand = numpy.random.randint
        shapes = [rand(2, 5) for i in range(5)]
        a, b = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
        dtypes = [int, float, '>i4', 'B']
        for i in range(1, len(shapes) + 1):
            shape = shapes[:i]
            for dt in dtypes:
                A = numpy.empty(shape, dtype=dt)
                a.send(A, copy=False)
                msg = b.recv(copy=False)

                B = numpy.frombuffer(msg, A.dtype).reshape(A.shape)
                assert_array_equal(A, B)

            A = numpy.empty(shape, dtype=[('a', int), ('b', float), ('c', 'a32')])
            A['a'] = 1024
            A['b'] = 1e9
            A['c'] = 'hello there'
            a.send(A, copy=False)
            msg = b.recv(copy=False)

            B = numpy.frombuffer(msg, A.dtype).reshape(A.shape)
            assert_array_equal(A, B)
Beispiel #2
0
 def test_buffer_numpy(self):
     """test non-copying numpy array messages"""
     try:
         import numpy
     except ImportError:
         raise SkipTest("requires numpy")
     if sys.version_info < (2,7):
         raise SkipTest("requires new-style buffer interface (py >= 2.7)")
     rand = numpy.random.randint
     shapes = [ rand(2,5) for i in range(5) ]
     a,b = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
     dtypes = [int, float, '>i4', 'B']
     for i in range(1,len(shapes)+1):
         shape = shapes[:i]
         for dt in dtypes:
             A = numpy.ndarray(shape, dtype=dt)
             while not (A < 1e400).all():
                 # don't let nan sneak in
                 A = numpy.ndarray(shape, dtype=dt)
             a.send(A, copy=False)
             msg = b.recv(copy=False)
             
             B = array_from_buffer(msg, A.dtype, A.shape)
             self.assertEquals(A.shape, B.shape)
             self.assertTrue((A==B).all())
         A = numpy.ndarray(shape, dtype=[('a', int), ('b', float), ('c', 'a32')])
         A['a'] = 1024
         A['b'] = 1e9
         A['c'] = 'hello there'
         a.send(A, copy=False)
         msg = b.recv(copy=False)
         
         B = array_from_buffer(msg, A.dtype, A.shape)
         self.assertEquals(A.shape, B.shape)
         self.assertTrue((A==B).all())
Beispiel #3
0
    def test_buffer_numpy(self):
        """test non-copying numpy array messages"""
        try:
            import numpy
        except ImportError:
            raise SkipTest("requires numpy")
        if sys.version_info < (2, 7):
            raise SkipTest("requires new-style buffer interface (py >= 2.7)")
        rand = numpy.random.randint
        shapes = [rand(2, 5) for i in range(5)]
        a, b = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
        dtypes = [int, float, '>i4', 'B']
        for i in range(1, len(shapes) + 1):
            shape = shapes[:i]
            for dt in dtypes:
                A = numpy.random.uniform(-10000000, 1000000,
                                         size=shape).astype(dt)
                a.send(A, copy=False)
                msg = b.recv(copy=False)

                B = numpy.frombuffer(msg, A.dtype).reshape(A.shape)
                self.assertEqual(A.shape, B.shape)
                self.assertTrue((A == B).all())
            A = numpy.empty(shape,
                            dtype=[('a', int), ('b', float), ('c', 'a32')])
            A['a'] = 1024
            A['b'] = 1e9
            A['c'] = 'hello there'
            a.send(A, copy=False)
            msg = b.recv(copy=False)

            B = numpy.frombuffer(msg, A.dtype).reshape(A.shape)
            self.assertEqual(A.shape, B.shape)
            self.assertTrue((A == B).all())
Beispiel #4
0
    def test_buffer_numpy(self):
        """test non-copying numpy array messages"""
        try:
            import numpy
            from numpy.testing import assert_array_equal
        except ImportError:
            raise SkipTest("requires numpy")
        if sys.version_info < (2,7):
            raise SkipTest("requires new-style buffer interface (py >= 2.7)")
        rand = numpy.random.randint
        shapes = [ rand(2,5) for i in range(5) ]
        a,b = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
        dtypes = [int, float, '>i4', 'B']
        for i in range(1,len(shapes)+1):
            shape = shapes[:i]
            for dt in dtypes:
                A = numpy.empty(shape, dtype=dt)
                a.send(A, copy=False)
                msg = b.recv(copy=False)

                B = numpy.frombuffer(msg, A.dtype).reshape(A.shape)
                assert_array_equal(A, B)

            A = numpy.empty(shape, dtype=[('a', int), ('b', float), ('c', 'a32')])
            A['a'] = 1024
            A['b'] = 1e9
            A['c'] = 'hello there'
            a.send(A, copy=False)
            msg = b.recv(copy=False)

            B = numpy.frombuffer(msg, A.dtype).reshape(A.shape)
            assert_array_equal(A, B)
Beispiel #5
0
    def test_multisend(self):
        """ensure that a message remains intact after multiple sends"""
        a, b = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
        s = b"message"
        m = zmq.Frame(s)
        self.assertEqual(s, m.bytes)

        a.send(m, copy=False)
        time.sleep(0.1)
        self.assertEqual(s, m.bytes)
        a.send(m, copy=False)
        time.sleep(0.1)
        self.assertEqual(s, m.bytes)
        a.send(m, copy=True)
        time.sleep(0.1)
        self.assertEqual(s, m.bytes)
        a.send(m, copy=True)
        time.sleep(0.1)
        self.assertEqual(s, m.bytes)
        for i in range(4):
            r = b.recv()
            self.assertEqual(s, r)
        self.assertEqual(s, m.bytes)
Beispiel #6
0
 def test_multisend(self):
     """ensure that a message remains intact after multiple sends"""
     a,b = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
     s = asbytes("message")
     m = zmq.Frame(s)
     self.assertEquals(s, m.bytes)
     
     a.send(m, copy=False)
     time.sleep(0.1)
     self.assertEquals(s, m.bytes)
     a.send(m, copy=False)
     time.sleep(0.1)
     self.assertEquals(s, m.bytes)
     a.send(m, copy=True)
     time.sleep(0.1)
     self.assertEquals(s, m.bytes)
     a.send(m, copy=True)
     time.sleep(0.1)
     self.assertEquals(s, m.bytes)
     for i in range(4):
         r = b.recv()
         self.assertEquals(s,r)
     self.assertEquals(s, m.bytes)