Python zmq.PAIR Examples
The following are 30
code examples of zmq.PAIR().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
zmq
, or try the search function
.

Example #1
Source Project: vnpy_crypto Author: birforce File: test_socket.py License: MIT License | 6 votes |
def test_send_unicode(self): "test sending unicode objects" a,b = self.create_bound_pair(zmq.PAIR, zmq.PAIR) self.sockets.extend([a,b]) u = "çπ§" if str is not unicode: u = u.decode('utf8') self.assertRaises(TypeError, a.send, u,copy=False) self.assertRaises(TypeError, a.send, u,copy=True) a.send_unicode(u) s = b.recv() self.assertEqual(s,u.encode('utf8')) self.assertEqual(s.decode('utf8'),u) a.send_unicode(u,encoding='utf16') s = b.recv_unicode(encoding='utf16') self.assertEqual(s,u)
Example #2
Source Project: vnpy_crypto Author: birforce File: test_poll.py License: MIT License | 6 votes |
def test_timeout(self): """make sure Poller.poll timeout has the right units (milliseconds).""" s1, s2 = self.create_bound_pair(zmq.PAIR, zmq.PAIR) poller = self.Poller() poller.register(s1, zmq.POLLIN) tic = time.time() evt = poller.poll(.005) toc = time.time() self.assertTrue(toc-tic < 0.1) tic = time.time() evt = poller.poll(5) toc = time.time() self.assertTrue(toc-tic < 0.1) self.assertTrue(toc-tic > .001) tic = time.time() evt = poller.poll(500) toc = time.time() self.assertTrue(toc-tic < 1) self.assertTrue(toc-tic > 0.1)
Example #3
Source Project: vnpy_crypto Author: birforce File: test_pair.py License: MIT License | 6 votes |
def test_multiple(self): s1, s2 = self.create_bound_pair(zmq.PAIR, zmq.PAIR) for i in range(10): msg = i*x s1.send(msg) for i in range(10): msg = i*x s2.send(msg) for i in range(10): msg = s1.recv() self.assertEqual(msg, i*x) for i in range(10): msg = s2.recv() self.assertEqual(msg, i*x)
Example #4
Source Project: vnpy_crypto Author: birforce File: test_monqueue.py License: MIT License | 6 votes |
def build_device(self, mon_sub=b"", in_prefix=b'in', out_prefix=b'out'): self.device = devices.ThreadMonitoredQueue(zmq.PAIR, zmq.PAIR, zmq.PUB, in_prefix, out_prefix) alice = self.context.socket(zmq.PAIR) bob = self.context.socket(zmq.PAIR) mon = self.context.socket(zmq.SUB) aport = alice.bind_to_random_port('tcp://127.0.0.1') bport = bob.bind_to_random_port('tcp://127.0.0.1') mport = mon.bind_to_random_port('tcp://127.0.0.1') mon.setsockopt(zmq.SUBSCRIBE, mon_sub) self.device.connect_in("tcp://127.0.0.1:%i"%aport) self.device.connect_out("tcp://127.0.0.1:%i"%bport) self.device.connect_mon("tcp://127.0.0.1:%i"%mport) self.device.start() time.sleep(.2) try: # this is currenlty necessary to ensure no dropped monitor messages # see LIBZMQ-248 for more info mon.recv_multipart(zmq.NOBLOCK) except zmq.ZMQError: pass self.sockets.extend([alice, bob, mon]) return alice, bob, mon
Example #5
Source Project: vnpy_crypto Author: birforce File: test_message.py License: MIT License | 6 votes |
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)
Example #6
Source Project: vnpy_crypto Author: birforce File: test_message.py License: MIT License | 6 votes |
def test_noncopying_recv(self): """check for clobbering message buffers""" null = b'\0'*64 sa,sb = self.create_bound_pair(zmq.PAIR, zmq.PAIR) for i in range(32): # try a few times sb.send(null, copy=False) m = sa.recv(copy=False) mb = m.bytes # buf = memoryview(m) buf = m.buffer del m for i in range(5): ff=b'\xff'*(40 + i*10) sb.send(ff, copy=False) m2 = sa.recv(copy=False) b = buf.tobytes() self.assertEqual(b, null) self.assertEqual(mb, null) self.assertEqual(m2.bytes, ff)
Example #7
Source Project: indy-plenum Author: hyperledger File: authenticator.py License: Apache License 2.0 | 6 votes |
def start(self): """Start the authentication thread""" # create a socket to communicate with auth thread. self.pipe = self.context.socket(zmq.PAIR) self.pipe.linger = 1 self.pipe.bind(self.pipe_endpoint) authenticator = MultiZapAuthenticator(self.context, encoding=self.encoding, log=self.log) self.thread = AuthenticationThread(self.context, self.pipe_endpoint, encoding=self.encoding, log=self.log, authenticator=authenticator) self.thread.start() # Event.wait:Changed in version 2.7: Previously, the method always returned None. if sys.version_info < (2, 7): self.thread.started.wait(timeout=10) else: if not self.thread.started.wait(timeout=10): raise RuntimeError("Authenticator thread failed to start")
Example #8
Source Project: indy-plenum Author: hyperledger File: authenticator.py License: Apache License 2.0 | 6 votes |
def start(self): """Start the authentication thread""" # create a socket to communicate with auth thread. self.pipe = self.context.socket(zmq.PAIR) self.pipe.linger = 1 self.pipe.bind(self.pipe_endpoint) authenticator = MultiZapAuthenticator(self.context, encoding=self.encoding, log=self.log) self.thread = AuthenticationThread(self.context, self.pipe_endpoint, encoding=self.encoding, log=self.log, authenticator=authenticator) self.thread.start() # Event.wait:Changed in version 2.7: Previously, the method always returned None. if sys.version_info < (2, 7): self.thread.started.wait(timeout=10) else: if not self.thread.started.wait(timeout=10): raise RuntimeError("Authenticator thread failed to start")
Example #9
Source Project: Computable Author: ktraunmueller File: test_session.py License: MIT License | 6 votes |
def test_send_raw(self): ctx = zmq.Context.instance() A = ctx.socket(zmq.PAIR) B = ctx.socket(zmq.PAIR) A.bind("inproc://test") B.connect("inproc://test") msg = self.session.msg('execute', content=dict(a=10)) msg_list = [self.session.pack(msg[part]) for part in ['header', 'parent_header', 'metadata', 'content']] self.session.send_raw(A, msg_list, ident=b'foo') ident, new_msg_list = self.session.feed_identities(B.recv_multipart()) new_msg = self.session.unserialize(new_msg_list) self.assertEqual(ident[0], b'foo') self.assertEqual(new_msg['msg_type'],msg['msg_type']) self.assertEqual(new_msg['header'],msg['header']) self.assertEqual(new_msg['parent_header'],msg['parent_header']) self.assertEqual(new_msg['content'],msg['content']) self.assertEqual(new_msg['metadata'],msg['metadata']) A.close() B.close() ctx.term()
Example #10
Source Project: testplan Author: Morgan-Stanley File: zmq_pair_connection.py License: Apache License 2.0 | 6 votes |
def get_multitest(name): test = MultiTest( name=name, suites=[ZMQTestsuite()], environment=[ # The server message pattern is defined as ZMQ PAIR. ZMQServer( name="server", host="127.0.0.1", port=0, message_pattern=zmq.PAIR, ), # The client message pattern is defined as ZMQ PAIR. ZMQClient( name="client", hosts=[context("server", "{{host}}")], ports=[context("server", "{{port}}")], message_pattern=zmq.PAIR, ), ], ) return test
Example #11
Source Project: adeptRL Author: heronsystems File: subproc_env_manager.py License: GNU General Public License v3.0 | 6 votes |
def zmq_robust_bind_socket(zmq_context): try_count = 0 while try_count < 3: try: socket = zmq_context.socket(zmq.PAIR) port = np.random.randint(5000, 30000) if ZMQ_CONNECT_METHOD == "tcp": socket.bind("tcp://*:{}".format(port)) if ZMQ_CONNECT_METHOD == "ipc": os.makedirs("/tmp/adeptzmq/", exist_ok=True) socket.bind("ipc:///tmp/adeptzmq/{}".format(port)) except zmq.error.ZMQError as e: try_count += 1 socket = None last_error = e continue break if socket is None: raise Exception( "ZMQ couldn't bind socket after 3 tries. {}".format(last_error) ) return socket, port
Example #12
Source Project: pySINDy Author: luckystarufo File: test_socket.py License: MIT License | 6 votes |
def test_send_unicode(self): "test sending unicode objects" a,b = self.create_bound_pair(zmq.PAIR, zmq.PAIR) self.sockets.extend([a,b]) u = "çπ§" if str is not unicode: u = u.decode('utf8') self.assertRaises(TypeError, a.send, u,copy=False) self.assertRaises(TypeError, a.send, u,copy=True) a.send_unicode(u) s = b.recv() self.assertEqual(s,u.encode('utf8')) self.assertEqual(s.decode('utf8'),u) a.send_unicode(u,encoding='utf16') s = b.recv_unicode(encoding='utf16') self.assertEqual(s,u)
Example #13
Source Project: pySINDy Author: luckystarufo File: test_poll.py License: MIT License | 6 votes |
def test_timeout(self): """make sure Poller.poll timeout has the right units (milliseconds).""" s1, s2 = self.create_bound_pair(zmq.PAIR, zmq.PAIR) poller = self.Poller() poller.register(s1, zmq.POLLIN) tic = time.time() evt = poller.poll(.005) toc = time.time() self.assertTrue(toc-tic < 0.1) tic = time.time() evt = poller.poll(5) toc = time.time() self.assertTrue(toc-tic < 0.1) self.assertTrue(toc-tic > .001) tic = time.time() evt = poller.poll(500) toc = time.time() self.assertTrue(toc-tic < 1) self.assertTrue(toc-tic > 0.1)
Example #14
Source Project: pySINDy Author: luckystarufo File: test_pair.py License: MIT License | 6 votes |
def test_multiple(self): s1, s2 = self.create_bound_pair(zmq.PAIR, zmq.PAIR) for i in range(10): msg = i*x s1.send(msg) for i in range(10): msg = i*x s2.send(msg) for i in range(10): msg = s1.recv() self.assertEqual(msg, i*x) for i in range(10): msg = s2.recv() self.assertEqual(msg, i*x)
Example #15
Source Project: pySINDy Author: luckystarufo File: test_monqueue.py License: MIT License | 6 votes |
def build_device(self, mon_sub=b"", in_prefix=b'in', out_prefix=b'out'): self.device = devices.ThreadMonitoredQueue(zmq.PAIR, zmq.PAIR, zmq.PUB, in_prefix, out_prefix) alice = self.context.socket(zmq.PAIR) bob = self.context.socket(zmq.PAIR) mon = self.context.socket(zmq.SUB) aport = alice.bind_to_random_port('tcp://127.0.0.1') bport = bob.bind_to_random_port('tcp://127.0.0.1') mport = mon.bind_to_random_port('tcp://127.0.0.1') mon.setsockopt(zmq.SUBSCRIBE, mon_sub) self.device.connect_in("tcp://127.0.0.1:%i"%aport) self.device.connect_out("tcp://127.0.0.1:%i"%bport) self.device.connect_mon("tcp://127.0.0.1:%i"%mport) self.device.start() time.sleep(.2) try: # this is currenlty necessary to ensure no dropped monitor messages # see LIBZMQ-248 for more info mon.recv_multipart(zmq.NOBLOCK) except zmq.ZMQError: pass self.sockets.extend([alice, bob, mon]) return alice, bob, mon
Example #16
Source Project: pySINDy Author: luckystarufo File: test_message.py License: MIT License | 6 votes |
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)
Example #17
Source Project: pySINDy Author: luckystarufo File: test_message.py License: MIT License | 6 votes |
def test_noncopying_recv(self): """check for clobbering message buffers""" null = b'\0'*64 sa,sb = self.create_bound_pair(zmq.PAIR, zmq.PAIR) for i in range(32): # try a few times sb.send(null, copy=False) m = sa.recv(copy=False) mb = m.bytes # buf = memoryview(m) buf = m.buffer del m for i in range(5): ff=b'\xff'*(40 + i*10) sb.send(ff, copy=False) m2 = sa.recv(copy=False) b = buf.tobytes() self.assertEqual(b, null) self.assertEqual(mb, null) self.assertEqual(m2.bytes, ff)
Example #18
Source Project: powerapi Author: powerapi-ng File: socket_interface.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def setup(self): """ Initialize sockets and send the selected port number to the father process with a Pipe """ # create the pull socket (to communicate with this actor, others # process have to connect a push socket to this socket) self.pull_socket, pull_port = self._create_socket(zmq.PULL, -1) # create the control socket (to control this actor, a process have to # connect a pair socket to this socket with the `control` method) self.control_socket, ctrl_port = self._create_socket(zmq.PAIR, 0) self.pull_socket_address = LOCAL_ADDR + ':' + str(pull_port) self.control_socket_address = LOCAL_ADDR + ':' + str(ctrl_port) self._pull_port.value = pull_port self._ctrl_port.value = ctrl_port self._values_available.set()
Example #19
Source Project: powerapi Author: powerapi-ng File: socket_interface.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def connect_control(self): """ Connect to the control socket of this actor Open a pair socket on the process that want to control this actor this method shouldn't be called if socket interface was not initialized with the setup method """ if self.pull_socket_address is None: self._values_available.wait() self.pull_socket_address = LOCAL_ADDR + ':' + str(self._pull_port.value) self.control_socket_address = LOCAL_ADDR + ':' + str(self._ctrl_port.value) self.control_socket = SafeContext.get_context().socket(zmq.PAIR) self.control_socket.setsockopt(zmq.LINGER, 0) self.control_socket.set_hwm(0) self.control_socket.connect(self.control_socket_address) self.logger.debug("connected control to %s" % (self.control_socket_address))
Example #20
Source Project: adviser Author: DigitalPhonetics File: engagement_tracker.py License: GNU General Public License v3.0 | 6 votes |
def __init__(self, domain="", camera_id: int = 0, openface_port: int = 6004, delay: int = 2, identifier=None): """ Args: camera_id: index of the camera you want to use (if you only have one camera: 0) """ Service.__init__(self, domain="", identifier=identifier) self.camera_id = camera_id self.openface_port = openface_port self.openface_running = False self.threshold = delay # provide number of seconds as parameter, one second = 15 frames ctx = Context.instance() self.openface_endpoint = ctx.socket(zmq.PAIR) self.openface_endpoint.bind(f"tcp://127.0.0.1:{self.openface_port}") startExtraction = f"{os.path.join(get_root_dir(), 'tools/OpenFace/build/bin/FaceLandmarkVidZMQ')} -device {self.camera_id} -port 6004" # todo config open face port self.p_openface = subprocess.Popen(startExtraction.split(), stdout=subprocess.PIPE) # start OpenFace self.extracting = False self.extractor_thread = None
Example #21
Source Project: funcX Author: funcx-faas File: zhelpers.py License: Apache License 2.0 | 5 votes |
def zpipe(ctx): """build inproc pipe for talking to threads mimic pipe used in czmq zthread_fork. Returns a pair of PAIRs connected via inproc """ a = ctx.socket(zmq.PAIR) b = ctx.socket(zmq.PAIR) a.linger = b.linger = 0 a.hwm = b.hwm = 1 iface = "inproc://%s" % binascii.hexlify(os.urandom(8)) a.bind(iface) b.connect(iface) return a,b
Example #22
Source Project: vnpy_crypto Author: birforce File: thread.py License: MIT License | 5 votes |
def __init__(self, context, endpoint, encoding='utf-8', log=None, authenticator=None): super(AuthenticationThread, self).__init__() self.context = context or zmq.Context.instance() self.encoding = encoding self.log = log = log or logging.getLogger('zmq.auth') self.started = Event() self.authenticator = authenticator or Authenticator(context, encoding=encoding, log=log) # create a socket to communicate back to main thread. self.pipe = context.socket(zmq.PAIR) self.pipe.linger = 1 self.pipe.connect(endpoint)
Example #23
Source Project: vnpy_crypto Author: birforce File: thread.py License: MIT License | 5 votes |
def start(self): """Start the authentication thread""" # create a socket to communicate with auth thread. self.pipe = self.context.socket(zmq.PAIR) self.pipe.linger = 1 self.pipe.bind(self.pipe_endpoint) self.thread = AuthenticationThread(self.context, self.pipe_endpoint, encoding=self.encoding, log=self.log) self.thread.start() # Event.wait:Changed in version 2.7: Previously, the method always returned None. if sys.version_info < (2,7): self.thread.started.wait(timeout=10) else: if not self.thread.started.wait(timeout=10): raise RuntimeError("Authenticator thread failed to start")
Example #24
Source Project: vnpy_crypto Author: birforce File: socket.py License: MIT License | 5 votes |
def get_monitor_socket(self, events=None, addr=None): """Return a connected PAIR socket ready to receive the event notifications. .. versionadded:: libzmq-4.0 .. versionadded:: 14.0 Parameters ---------- events : int [default: ZMQ_EVENTS_ALL] The bitmask defining which events are wanted. addr : string [default: None] The optional endpoint for the monitoring sockets. Returns ------- socket : (PAIR) The socket is already connected and ready to receive messages. """ # safe-guard, method only available on libzmq >= 4 if zmq.zmq_version_info() < (4,): raise NotImplementedError("get_monitor_socket requires libzmq >= 4, have %s" % zmq.zmq_version()) # if already monitoring, return existing socket if self._monitor_socket: if self._monitor_socket.closed: self._monitor_socket = None else: return self._monitor_socket if addr is None: # create endpoint name from internal fd addr = "inproc://monitor.s-%d" % self.FD if events is None: # use all events events = zmq.EVENT_ALL # attach monitoring socket self.monitor(addr, events) # create new PAIR socket and connect it self._monitor_socket = self.context.socket(zmq.PAIR) self._monitor_socket.connect(addr) return self._monitor_socket
Example #25
Source Project: vnpy_crypto Author: birforce File: socket.py License: MIT License | 5 votes |
def disable_monitor(self): """Shutdown the PAIR socket (created using get_monitor_socket) that is serving socket events. .. versionadded:: 14.4 """ self._monitor_socket = None self.monitor(None, 0)
Example #26
Source Project: vnpy_crypto Author: birforce File: test_poll.py License: MIT License | 5 votes |
def test_pair(self): s1, s2 = self.create_bound_pair(zmq.PAIR, zmq.PAIR) # Sleep to allow sockets to connect. wait() poller = self.Poller() poller.register(s1, zmq.POLLIN|zmq.POLLOUT) poller.register(s2, zmq.POLLIN|zmq.POLLOUT) # Poll result should contain both sockets socks = dict(poller.poll()) # Now make sure that both are send ready. self.assertEqual(socks[s1], zmq.POLLOUT) self.assertEqual(socks[s2], zmq.POLLOUT) # Now do a send on both, wait and test for zmq.POLLOUT|zmq.POLLIN s1.send(b'msg1') s2.send(b'msg2') wait() socks = dict(poller.poll()) self.assertEqual(socks[s1], zmq.POLLOUT|zmq.POLLIN) self.assertEqual(socks[s2], zmq.POLLOUT|zmq.POLLIN) # Make sure that both are in POLLOUT after recv. s1.recv() s2.recv() socks = dict(poller.poll()) self.assertEqual(socks[s1], zmq.POLLOUT) self.assertEqual(socks[s2], zmq.POLLOUT) poller.unregister(s1) poller.unregister(s2)
Example #27
Source Project: vnpy_crypto Author: birforce File: test_poll.py License: MIT License | 5 votes |
def test_no_events(self): s1, s2 = self.create_bound_pair(zmq.PAIR, zmq.PAIR) poller = self.Poller() poller.register(s1, zmq.POLLIN|zmq.POLLOUT) poller.register(s2, 0) self.assertTrue(s1 in poller) self.assertFalse(s2 in poller) poller.register(s1, 0) self.assertFalse(s1 in poller)
Example #28
Source Project: vnpy_crypto Author: birforce File: test_poll.py License: MIT License | 5 votes |
def test_pair(self): s1, s2 = self.create_bound_pair(zmq.PAIR, zmq.PAIR) # Sleep to allow sockets to connect. wait() rlist, wlist, xlist = zmq.select([s1, s2], [s1, s2], [s1, s2]) self.assert_(s1 in wlist) self.assert_(s2 in wlist) self.assert_(s1 not in rlist) self.assert_(s2 not in rlist)
Example #29
Source Project: vnpy_crypto Author: birforce File: test_poll.py License: MIT License | 5 votes |
def test_timeout(self): """make sure select timeout has the right units (seconds).""" s1, s2 = self.create_bound_pair(zmq.PAIR, zmq.PAIR) tic = time.time() r,w,x = zmq.select([s1,s2],[],[],.005) toc = time.time() self.assertTrue(toc-tic < 1) self.assertTrue(toc-tic > 0.001) tic = time.time() r,w,x = zmq.select([s1,s2],[],[],.25) toc = time.time() self.assertTrue(toc-tic < 1) self.assertTrue(toc-tic > 0.1)
Example #30
Source Project: vnpy_crypto Author: birforce File: test_poll.py License: MIT License | 5 votes |
def test_socket_poll(self): s1, s2 = self.create_bound_pair(zmq.PAIR, zmq.PAIR) tic = time.time() r = gevent.spawn(lambda: s2.poll(10000)) s = gevent.spawn(lambda: s1.send(b'msg1')) r.join() toc = time.time() self.assertTrue(toc-tic < 1)