本文整理汇总了Python中zmq.Context方法的典型用法代码示例。如果您正苦于以下问题:Python zmq.Context方法的具体用法?Python zmq.Context怎么用?Python zmq.Context使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zmq
的用法示例。
在下文中一共展示了zmq.Context方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Context [as 别名]
def run(self):
enable_death_signal(_warn=self.idx == 0)
self.ds.reset_state()
itr = _repeat_iter(lambda: self.ds)
context = zmq.Context()
socket = context.socket(zmq.PUSH)
socket.set_hwm(self.hwm)
socket.connect(self.conn_name)
try:
while True:
try:
dp = next(itr)
socket.send(dumps(dp), copy=False)
except Exception:
dp = _ExceptionWrapper(sys.exc_info()).pack()
socket.send(dumps(dp), copy=False)
raise
# sigint could still propagate here, e.g. when nested
except KeyboardInterrupt:
pass
finally:
socket.close(0)
context.destroy(0)
示例2: reset_state
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Context [as 别名]
def reset_state(self):
_MultiProcessZMQDataFlow.reset_state(self)
_ParallelMapData.reset_state(self)
self._guard = DataFlowReentrantGuard()
self.context = zmq.Context()
self.socket = self.context.socket(zmq.DEALER)
self.socket.set_hwm(self._buffer_size * 2)
pipename = _get_pipe_name('dataflow-map')
_bind_guard(self.socket, pipename)
self._proc_ids = [u'{}'.format(k).encode('utf-8') for k in range(self.num_proc)]
worker_hwm = int(self._buffer_size * 2 // self.num_proc)
self._procs = [self._create_worker(self._proc_ids[k], pipename, worker_hwm)
for k in range(self.num_proc)]
self._start_processes()
self._fill_buffer() # pre-fill the bufer
示例3: run
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Context [as 别名]
def run(self):
enable_death_signal(_warn=self.identity == b'0')
ctx = zmq.Context()
# recv jobs
socket = ctx.socket(zmq.PULL)
socket.setsockopt(zmq.IDENTITY, self.identity)
socket.set_hwm(self.hwm * self.batch_size)
socket.connect(self.input_pipe)
# send results
out_socket = ctx.socket(zmq.PUSH)
out_socket.set_hwm(max(self.hwm, 5))
out_socket.connect(self.result_pipe)
batch = []
while True:
dp = loads(socket.recv(copy=False))
dp = self.map_func(dp)
if dp is not None:
batch.append(dp)
if len(batch) == self.batch_size:
dp = BatchData.aggregate_batch(batch)
out_socket.send(dumps(dp), copy=False)
del batch[:]
示例4: __init__
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Context [as 别名]
def __init__(self, max_nprocs):
import subprocess
import sys
import os
import zmq
# Since the output terminals are used for lots of debug output etc., we use
# ZeroMQ to communicate with the workers.
zctx = zmq.Context()
socket = zctx.socket(zmq.REQ)
port = socket.bind_to_random_port("tcp://*")
cmd = 'import %s as mod; mod._mpi_worker("tcp://127.0.0.1:%d")' % (__name__, port)
env = dict(os.environ)
env['PYTHONPATH'] = ':'.join(sys.path)
self.child = subprocess.Popen(['mpiexec', '-np', str(max_nprocs), sys.executable,
'-c', cmd], env=env)
self.socket = socket
示例5: __init__
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Context [as 别名]
def __init__(self, ip_address, port_range):
"""
Parameters
----------
ip_address: str
IP address of the client (where Parsl runs)
port_range: tuple(int, int)
Port range for the comms between client and interchange
"""
self.context = zmq.Context()
self.zmq_socket = self.context.socket(zmq.DEALER)
self.zmq_socket.set_hwm(0)
self.port = self.zmq_socket.bind_to_random_port("tcp://{}".format(ip_address),
min_port=port_range[0],
max_port=port_range[1])
示例6: run
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Context [as 别名]
def run(self):
context = zmq.Context()
receiver = context.socket(zmq.PULL)
receiver.connect(self.worker_address)
sink = context.socket(zmq.PUSH)
sink.connect(self.sink_address)
input_fn = self.input_fn_builder(receiver)
self.logger.info('ready and listening')
start_t = time.perf_counter()
for r in self.estimator.predict(input_fn, yield_single_examples=False):
# logger.info('new result!')
send_ndarray(sink, r['client_id'], r['encodes'])
time_used = time.perf_counter() - start_t
start_t = time.perf_counter()
self.logger.info('job %s\tsamples: %4d\tdone: %.2fs' %
(r['client_id'], r['encodes'].shape[0], time_used))
receiver.close()
sink.close()
context.term()
self.logger.info('terminated!')
示例7: _get_context
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Context [as 别名]
def _get_context(self, *args, **kwargs):
'''
Find the ``zmq.Context`` from ``args`` and ``kwargs`` at call time.
First, if there is an keyword argument named ``context`` and it is a
``zmq.Context`` instance , we will take it.
Second, we check all the ``args``, take the first ``zmq.Context``
instance.
Finally, we will provide default Context -- ``zmq.Context.instance``
:return: a ``zmq.Context`` instance
'''
if self.context_name in kwargs:
ctx = kwargs[self.context_name]
if isinstance(ctx, zmq.Context):
return ctx
for arg in args:
if isinstance(arg, zmq.Context):
return arg
# not specified by any decorator
return zmq.Context.instance()
示例8: test_poll_base_socket
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Context [as 别名]
def test_poll_base_socket(self):
@asyncio.coroutine
def test():
ctx = zmq.Context()
url = 'inproc://test'
a = ctx.socket(zmq.PUSH)
b = ctx.socket(zmq.PULL)
self.sockets.extend([a, b])
a.bind(url)
b.connect(url)
poller = zaio.Poller()
poller.register(b, zmq.POLLIN)
f = poller.poll(timeout=1000)
assert not f.done()
a.send_multipart([b'hi', b'there'])
evt = yield from f
self.assertEqual(evt, [(b, zmq.POLLIN)])
recvd = b.recv_multipart()
self.assertEqual(recvd, [b'hi', b'there'])
self.loop.run_until_complete(test())
示例9: tearDown
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Context [as 别名]
def tearDown(self):
contexts = set([self.context])
while self.sockets:
sock = self.sockets.pop()
contexts.add(sock.context) # in case additional contexts are created
sock.close(0)
for ctx in contexts:
t = Thread(target=ctx.term)
t.daemon = True
t.start()
t.join(timeout=2)
if t.is_alive():
# reset Context.instance, so the failure to term doesn't corrupt subsequent tests
zmq.sugar.context.Context._instance = None
raise RuntimeError("context could not terminate, open sockets likely remain in test")
super(BaseZMQTestCase, self).tearDown()
示例10: test_gc
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Context [as 别名]
def test_gc(self):
"""test close&term by garbage collection alone"""
if PYPY:
raise SkipTest("GC doesn't work ")
# test credit @dln (GH #137):
def gcf():
def inner():
ctx = self.Context()
s = ctx.socket(zmq.PUSH)
inner()
gc.collect()
t = Thread(target=gcf)
t.start()
t.join(timeout=1)
self.assertFalse(t.is_alive(), "Garbage collection should have cleaned up context")
示例11: test_cyclic_destroy
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Context [as 别名]
def test_cyclic_destroy(self):
"""ctx.destroy should succeed when cyclic ref prevents gc"""
# test credit @dln (GH #137):
class CyclicReference(object):
def __init__(self, parent=None):
self.parent = parent
def crash(self, sock):
self.sock = sock
self.child = CyclicReference(self)
def crash_zmq():
ctx = self.Context()
sock = ctx.socket(zmq.PULL)
c = CyclicReference()
c.crash(sock)
ctx.destroy()
crash_zmq()
示例12: test_shadow
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Context [as 别名]
def test_shadow(self):
ctx = self.Context()
ctx2 = self.Context.shadow(ctx.underlying)
self.assertEqual(ctx.underlying, ctx2.underlying)
s = ctx.socket(zmq.PUB)
s.close()
del ctx2
self.assertFalse(ctx.closed)
s = ctx.socket(zmq.PUB)
ctx2 = self.Context.shadow(ctx.underlying)
s2 = ctx2.socket(zmq.PUB)
s.close()
s2.close()
ctx.term()
self.assertRaisesErrno(zmq.EFAULT, ctx2.socket, zmq.PUB)
del ctx2
示例13: test_shadow_pyczmq
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Context [as 别名]
def test_shadow_pyczmq(self):
try:
from pyczmq import zctx, zsocket, zstr
except Exception:
raise SkipTest("Requires pyczmq")
ctx = zctx.new()
a = zsocket.new(ctx, zmq.PUSH)
zsocket.bind(a, "inproc://a")
ctx2 = self.Context.shadow_pyczmq(ctx)
b = ctx2.socket(zmq.PULL)
b.connect("inproc://a")
zstr.send(a, b'hi')
rcvd = self.recv(b)
self.assertEqual(rcvd, b'hi')
b.close()
示例14: test_ctx_multi_thread
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Context [as 别名]
def test_ctx_multi_thread():
@context()
@context()
def f(foo, bar):
assert isinstance(foo, zmq.Context), foo
assert isinstance(bar, zmq.Context), bar
assert len(set(map(id, [foo, bar]))) == 2, set(map(id, [foo, bar]))
threads = [threading.Thread(target=f) for i in range(8)]
[t.start() for t in threads]
[t.join() for t in threads]
##############################################
# Test cases for @socket
##############################################
示例15: test_multi_skts
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Context [as 别名]
def test_multi_skts():
@socket(zmq.PUB)
@socket(zmq.SUB)
@socket(zmq.PUSH)
def test(pub, sub, push):
assert isinstance(pub, zmq.Socket), pub
assert isinstance(sub, zmq.Socket), sub
assert isinstance(push, zmq.Socket), push
assert pub.context is zmq.Context.instance()
assert sub.context is zmq.Context.instance()
assert push.context is zmq.Context.instance()
assert pub.type == zmq.PUB
assert sub.type == zmq.SUB
assert push.type == zmq.PUSH
test()