本文整理汇总了Python中zmq.PAIR属性的典型用法代码示例。如果您正苦于以下问题:Python zmq.PAIR属性的具体用法?Python zmq.PAIR怎么用?Python zmq.PAIR使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类zmq
的用法示例。
在下文中一共展示了zmq.PAIR属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_send_unicode
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import PAIR [as 别名]
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)
示例2: test_timeout
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import PAIR [as 别名]
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)
示例3: test_multiple
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import PAIR [as 别名]
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)
示例4: build_device
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import PAIR [as 别名]
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
示例5: test_multisend
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import PAIR [as 别名]
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)
示例6: test_noncopying_recv
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import PAIR [as 别名]
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)
示例7: start
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import PAIR [as 别名]
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")
示例8: test_send_raw
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import PAIR [as 别名]
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()
示例9: get_multitest
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import PAIR [as 别名]
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
示例10: zmq_robust_bind_socket
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import PAIR [as 别名]
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
示例11: setup
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import PAIR [as 别名]
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()
示例12: connect_control
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import PAIR [as 别名]
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))
示例13: __init__
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import PAIR [as 别名]
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
示例14: zpipe
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import PAIR [as 别名]
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
示例15: __init__
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import PAIR [as 别名]
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)