示例#1
0
 def test_buffer_out(self):
     """receiving buffered output"""
     if unicode is str:
         ins = "§§¶•ªº˜µ¬˚…∆˙åß∂©œ∑´†≈ç√".encode('utf8')
     else:
         ins = "§§¶•ªº˜µ¬˚…∆˙åß∂©œ∑´†≈ç√"
     m = zmq.Message(ins)
     outb = m.buffer
     self.assertTrue(isinstance(outb, view))
     self.assert_(outb is m.buffer)
     self.assert_(m.buffer is m.buffer)
示例#2
0
 def test_unicode(self):
     """Test the unicode representations of the Messages."""
     s = unicode('asdf')
     self.assertRaises(TypeError, zmq.Message, s)
     u = '§'
     if str is not unicode:
         u = u.decode('utf8')
     for i in range(16):
         s = (2**i)*u
         m = zmq.Message(s.encode('utf8'))
         self.assertEquals(s, unicode(m.bytes,'utf8'))
示例#3
0
 def test_bytes(self):
     """Test the Message.bytes property."""
     for i in range(1,16):
         s = (2**i)*x
         m = zmq.Message(s)
         b = m.bytes
         self.assertEquals(s, m.bytes)
         # check that it copies
         self.assert_(b is not s)
         # check that it copies only once
         self.assert_(b is m.bytes)
示例#4
0
    def test_memoryview(self):
        """test messages from memoryview"""
        major,minor = sys.version_info[:2]
        if not (major >= 3 or (major == 2 and minor >= 7)):
            raise SkipTest("memoryviews only in python >= 2.7")

        s = asbytes('carrotjuice')
        v = memoryview(s)
        m = zmq.Message(s)
        buf = m.buffer
        s2 = buf.tobytes()
        self.assertEquals(s2,s)
        self.assertEquals(m.bytes,s)
示例#5
0
    def test_memoryview(self):
        """test messages from memoryview (only valid for python >= 2.7)"""
        major,minor = sys.version_info[:2]
        if not (major >= 3 or (major == 2 and minor >= 7)):
            raise SkipTest

        s = 'carrotjuice'.encode()
        v = memoryview(s)
        m = zmq.Message(s)
        buf = m.buffer
        s2 = buf.tobytes()
        self.assertEquals(s2,s)
        self.assertEquals(m.bytes,s)
示例#6
0
 def test_buffer_numpy(self):
     """test non-copying numpy array messages"""
     try:
         import numpy
     except ImportError:
         raise SkipTest
     shapes = map(numpy.random.randint, [2]*5,[16]*5)
     for i in range(1,len(shapes)+1):
         shape = shapes[:i]
         A = numpy.random.random(shape)
         m = zmq.Message(A)
         self.assertEquals(A.data, m.buffer)
         B = numpy.frombuffer(m.buffer,dtype=A.dtype).reshape(A.shape)
         self.assertEquals((A==B).all(), True)
 def test_tracking(self):
     """test tracking messages"""
     a, b = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
     s = self.session
     stream = ZMQStream(a)
     msg = s.send(a, 'hello', track=False)
     self.assertTrue(msg['tracker'] is None)
     msg = s.send(a, 'hello', track=True)
     self.assertTrue(isinstance(msg['tracker'], zmq.MessageTracker))
     M = zmq.Message(b'hi there', track=True)
     msg = s.send(a, 'hello', buffers=[M], track=True)
     t = msg['tracker']
     self.assertTrue(isinstance(t, zmq.MessageTracker))
     self.assertRaises(zmq.NotDone, t.wait, .1)
     del M
     t.wait(1)  # this will raise
 def test_tracking(self):
     """test tracking messages"""
     a, b = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
     s = self.session
     s.copy_threshold = 1
     ZMQStream(a)
     msg = s.send(a, "hello", track=False)
     self.assertTrue(msg["tracker"] is ss.DONE)
     msg = s.send(a, "hello", track=True)
     self.assertTrue(isinstance(msg["tracker"], zmq.MessageTracker))
     M = zmq.Message(b"hi there", track=True)
     msg = s.send(a, "hello", buffers=[M], track=True)
     t = msg["tracker"]
     self.assertTrue(isinstance(t, zmq.MessageTracker))
     self.assertRaises(zmq.NotDone, t.wait, 0.1)
     del M
     t.wait(1)  # this will raise
示例#9
0
def main(argv):
    use_poll = '-p' in argv
    use_copy = '-c' in argv
    if use_copy:
        argv.remove('-c')
    if use_poll:
        argv.remove('-p')

    if len(argv) != 4:
        print(
            'usage: remote_thr [-c use-copy] [-p use-poll] <connect-to> <message-size> <message-count>'
        )
        sys.exit(1)

    try:
        connect_to = argv[1]
        message_size = int(argv[2])
        message_count = int(argv[3])
    except (ValueError, OverflowError):
        print('message-size and message-count must be integers')
        sys.exit(1)

    ctx = zmq.Context()
    s = ctx.socket(zmq.PUB)

    #  Add your socket options here.
    #  For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM.

    if use_poll:
        p = zmq.Poller()
        p.register(s)

    s.connect(connect_to)

    # remove the b for Python2.5:
    msg = zmq.Message(b' ' * message_size)
    block = zmq.NOBLOCK if use_poll else 0

    for i in range(message_count):
        if use_poll:
            res = p.poll()
            assert (res[0][1] & zmq.POLLOUT)
        s.send(msg, block, copy=use_copy)

    s.close()
    ctx.term()
示例#10
0
 def test_lifecycle2(self):
     """Run through a different ref counting cycle with a copy."""
     for i in range(5, 16):  # 32, 64,..., 65536
         s = (2**i) * 'x'
         self.assertEquals(grc(s), 2)
         m = zmq.Message(s)
         self.assertEquals(grc(s), 4)
         m2 = copy.copy(m)
         self.assertEquals(grc(s), 5)
         self.assertEquals(s, str(m))
         self.assertEquals(s, str(m2))
         self.assert_(s is str(m))
         self.assert_(s is str(m2))
         del m
         self.assertEquals(grc(s), 4)
         del m2
         self.assertEquals(grc(s), 2)
         del s
示例#11
0
 def test_buffer_numpy(self):
     """test non-copying numpy array messages"""
     try:
         import numpy
     except ImportError:
         raise SkipTest
     rand = numpy.random.randint
     shapes = [ rand(2,16) for i in range(5) ]
     for i in range(1,len(shapes)+1):
         shape = shapes[:i]
         A = numpy.random.random(shape)
         m = zmq.Message(A)
         if view.__name__ == 'buffer':
             self.assertEquals(A.data, m.buffer)
             B = numpy.frombuffer(m.buffer,dtype=A.dtype).reshape(A.shape)
         else:
             self.assertEquals(memoryview(A), m.buffer)
             B = numpy.array(m.buffer,dtype=A.dtype).reshape(A.shape)
         self.assertEquals((A==B).all(), True)
示例#12
0
    def test_lifecycle1(self):
        """Run through a ref counting cycle with a copy."""
        try:
            view = memoryview
        except NameError:
            view = type(None)
        for i in range(5, 16):  # 32, 64,..., 65536
            s = (2**i)*x
            rc = 2
            self.assertEquals(grc(s), rc)
            m = zmq.Message(s)
            rc += 2
            self.assertEquals(grc(s), rc)
            m2 = copy.copy(m)
            rc += 1
            self.assertEquals(grc(s), rc)
            b = m2.buffer
            extra = int(isinstance(b,view))
            # memoryview incs by 2
            # buffer by 1
            rc += 1+extra
            self.assertEquals(grc(s), rc)

            self.assertEquals(s, str(m).encode())
            self.assertEquals(s, str(m2).encode())
            self.assertEquals(s, m.bytes)
            # self.assert_(s is str(m))
            # self.assert_(s is str(m2))
            del m2
            rc -= 1
            self.assertEquals(grc(s), rc)
            rc -= 1+extra
            del b
            self.assertEquals(grc(s), rc)
            del m
            rc -= 2
            self.assertEquals(grc(s), rc)
            self.assertEquals(rc, 2)
            del s
示例#13
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 = "message".encode()
     m = zmq.Message(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)
示例#14
0
#    the terms of the Lesser GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    pyzmq is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    Lesser GNU General Public License for more details.
#
#    You should have received a copy of the Lesser GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import zmq
import os
from time import time

print ('Client %i'%os.getpid())

context = zmq.Context(1)

socket = context.socket(zmq.REQ)
socket.connect('tcp://127.0.0.1:5555')

while True:
    data = zmq.Message(str(os.getpid()).encode('utf8'))
    start = time()
    socket.send(data)
    data = socket.recv()
    print (time()-start, data)

示例#15
0
#    pyzmq is free software; you can redistribute it and/or modify it under
#    the terms of the Lesser GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    pyzmq is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    Lesser GNU General Public License for more details.
#
#    You should have received a copy of the Lesser GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import zmq
import os
from time import time

print 'Client', os.getpid()

context = zmq.Context(1)

socket = context.socket(zmq.REQ)
socket.connect('tcp://127.0.0.1:5555')

while True:
    data = zmq.Message(str(os.getpid()))
    start = time()
    socket.send(data)
    data = socket.recv()
    print time() - start, data
示例#16
0
 def test_len(self):
     """Test the len of the Messages."""
     for i in range(16):
         s = (2**i) * 'x'
         m = zmq.Message(s)
         self.assertEquals(len(s), len(s))
示例#17
0
# -*- coding: utf-8 -*-
import zmq

# Test zmq Frame's buffer protocol
try:
    buffer(zmq.Message(''))
    fbuffer = buffer
    bbytes = bytes
except TypeError:

    def fbuffer(frame):
        return frame.buffer if hasattr(frame, 'buffer') else buffer(frame)

    def bbytes(buf):
        return buf.tobytes() if hasattr(buf, 'tobytes') else bytes(buf)
示例#18
0
文件: zreplier.py 项目: amitu/zums
import zmq, threading, time, logging
try:
    import json
except ImportError:
    import simplejson as json

CONTEXT = zmq.Context()
ZNull = zmq.Message(None)

# multi helpers # {{{
def recv_multi(sock):
    parts = []
    while True:
        parts.append(sock.recv())
        if not sock.getsockopt(zmq.RCVMORE): break
    return parts

def send_multi(sock, parts, reply=None):
    if reply is not None:
        parts[-1] = reply
    for part in parts[:-1]:
        sock.send(part, zmq.SNDMORE)
    sock.send(parts[-1], 0)
# }}}

logger = logging.getLogger("zums.ZReplier")
class NoReply(Exception): pass

# ZReplier # {{{
class ZReplier(threading.Thread):
示例#19
0
 def test_str(self):
     """Test the str representations of the Messages."""
     for i in range(16):
         s = (2**i)*x
         m = zmq.Message(s)
         self.assertEquals(s, str(m).encode())
示例#20
0
SOFTWARE.
'''

import zmq 
from time import time 
import json 
from random import randrange 
import time 
import datetime 
 
context = zmq.Context() 
 
socket = context.socket(zmq.PUSH) 
socket.bind('tcp://127.0.0.1:5555') 
 
while True: 
#Generate some synthetic data
    data = str(randrange(0,60))  + ',' + str(randrange(0,100)) + ',' + str(randrange(5000,12000)) +  ',' + str(randrange(50,350)) 
              
    ts=time.time() 
    timestamp = 
    datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') 
 
    msg={'timestamp': timestamp, 'data': data} 
    print msg 
    data = zmq.Message(json.dumps(msg)) 
    socket.send(data) 
 
    print data 
    time.sleep(1)
示例#21
0
 def test_no_tracker(self):
     m = zmq.Message(asbytes('asdf'), track=False)
     self.assertEquals(m.tracker, None)
     m2 = copy.copy(m)
     self.assertEquals(m2.tracker, None)
     self.assertRaises(ValueError, zmq.MessageTracker, m)
示例#22
0
 def test_no_tracker(self):
     m = zmq.Message('asdf'.encode(), track=False)
     self.assertRaises(ValueError, getattr, m, 'done')
     m2 = copy.copy(m)
     self.assertRaises(ValueError, getattr, m2, 'done')
     self.assertRaises(ValueError, zmq.MessageTracker, m)