本文整理汇总了Python中zmq.asyncio方法的典型用法代码示例。如果您正苦于以下问题:Python zmq.asyncio方法的具体用法?Python zmq.asyncio怎么用?Python zmq.asyncio使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zmq
的用法示例。
在下文中一共展示了zmq.asyncio方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: kernel_terminated
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import asyncio [as 别名]
def kernel_terminated(app: web.Application, agent_id: AgentId, event_name: str,
kernel_id: KernelId, reason: str,
exit_code: int = None) -> None:
try:
kernel = await app['registry'].get_kernel(
kernel_id, (kernels.c.role, kernels.c.status), allow_stale=True)
except SessionNotFound:
return
if kernel.role == 'master':
session_name = kernel['sess_id']
stream_key = (session_name, kernel['access_key'])
cancelled_tasks = []
for sock in app['stream_stdin_socks'][session_name]:
sock.close()
for handler in list(app['stream_pty_handlers'].get(stream_key, [])):
handler.cancel()
cancelled_tasks.append(handler)
for handler in list(app['stream_execute_handlers'].get(stream_key, [])):
handler.cancel()
cancelled_tasks.append(handler)
for handler in list(app['stream_proxy_handlers'].get(stream_key, [])):
handler.cancel()
cancelled_tasks.append(handler)
await asyncio.gather(*cancelled_tasks, return_exceptions=True)
# TODO: reconnect if restarting?
示例2: launch
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import asyncio [as 别名]
def launch(self):
"""
Launches an asynchronous loop executors for respective task.
"""
# check if receive mode enabled
if self.__receive_mode:
if self.__logging:
logger.debug("Launching NetGear asynchronous generator!")
# run loop executor for Receiver asynchronous generator
self.loop.run_in_executor(None, self.recv_generator)
# return instance
return self
else:
# Otherwise launch Server handler
if self.__logging:
logger.debug("Creating NetGear asynchronous server handler!")
# create task for Server Handler
self.task = asyncio.ensure_future(self.__server_handler(), loop=self.loop)
# return instance
return self
示例3: __frame_generator
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import asyncio [as 别名]
def __frame_generator(self):
"""
Returns a default frame-generator for NetGear's Server Handler.
"""
# start stream
self.__stream.start()
# loop over stream until its terminated
while not self.__terminate:
# read frames
frame = self.__stream.read()
# break if NoneType
if frame is None:
break
# yield frame
yield frame
# sleep for sometime
await asyncio.sleep(0.00001)
示例4: _do_heartbeat
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import asyncio [as 别名]
def _do_heartbeat(self):
while True:
try:
if self._socket.getsockopt(zmq.TYPE) == zmq.ROUTER:
yield from self._do_router_heartbeat()
elif self._socket.getsockopt(zmq.TYPE) == zmq.DEALER:
yield from self._do_dealer_heartbeat()
yield from asyncio.sleep(self._heartbeat_interval,
loop=self._event_loop)
except CancelledError: # pylint: disable=try-except-raise
# The concurrent.futures.CancelledError is caught by asyncio
# when the Task associated with the coroutine is cancelled.
# The raise is required to stop this component.
raise
except Exception as e: # pylint: disable=broad-except
LOGGER.exception(
"An error occurred while sending heartbeat: %s", e)
示例5: shutdown
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import asyncio [as 别名]
def shutdown(self):
self._dispatcher.remove_send_message(self._connection)
self._dispatcher.remove_send_last_message(self._connection)
if self._event_loop is None:
return
if self._event_loop.is_closed():
return
if self._event_loop.is_running():
if self._auth is not None:
self._event_loop.call_soon_threadsafe(self._auth.stop)
else:
# event loop was never started, so the only Task that is running
# is the Auth Task.
self._event_loop.run_until_complete(self._stop_auth())
asyncio.ensure_future(self._stop(), loop=self._event_loop)
示例6: do_backoff
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import asyncio [as 别名]
def do_backoff(self, err_msg=" "):
if self.num_retries == self.max_retries:
LOGGER.warning("Failed sending message to the Validator. No more "
"retries left. Backoff terminated: %s",
err_msg)
raise self.error
self.num_retries += 1
LOGGER.warning("Sleeping for %s ms after failed attempt %s of %s to "
"send message to the Validator: %s",
str(self.num_retries),
str(self.max_retries),
str(self.interval / 1000),
err_msg)
await asyncio.sleep(self.interval / 1000)
self.interval *= 2
示例7: start
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import asyncio [as 别名]
def start(self):
"""Starts receiving messages on the underlying socket and passes them
to the message router.
"""
self._is_running = True
while self._is_running:
try:
zmq_msg = await self._socket.recv_multipart()
message = Message()
message.ParseFromString(zmq_msg[-1])
await self._msg_router.route_msg(message)
except DecodeError as e:
LOGGER.warning('Unable to decode: %s', e)
except zmq.ZMQError as e:
LOGGER.warning('Unable to receive: %s', e)
return
except asyncio.CancelledError:
self._is_running = False
示例8: launch
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import asyncio [as 别名]
def launch(self, websocket_port, message_max_size):
async def receive_from_engine_worker_loop():
await self.wait_for_start()
while self.is_running():
await self._receive_from_engine_worker_helper()
async def heartbeat_loop():
await self.wait_for_start()
while self.is_running():
await asyncio.sleep(self._timeout)
await self._heartbeat_helper()
asyncio.ensure_future(receive_from_engine_worker_loop())
asyncio.ensure_future(heartbeat_loop())
super().launch(websocket_port, message_max_size)
示例9: test_recv_json_cancelled
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import asyncio [as 别名]
def test_recv_json_cancelled(self):
@asyncio.coroutine
def test():
a, b = self.create_bound_pair(zmq.PUSH, zmq.PULL)
f = b.recv_json()
assert not f.done()
f.cancel()
# cycle eventloop to allow cancel events to fire
yield from asyncio.sleep(0)
obj = dict(a=5)
yield from a.send_json(obj)
with pytest.raises(CancelledError):
recvd = yield from f
assert f.done()
# give it a chance to incorrectly consume the event
events = yield from b.poll(timeout=5)
assert events
yield from asyncio.sleep(0)
# make sure cancelled recv didn't eat up event
f = b.recv_json()
recvd = yield from asyncio.wait_for(f, timeout=5)
assert recvd == obj
self.loop.run_until_complete(test())
示例10: test_custom_serialize_error
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import asyncio [as 别名]
def test_custom_serialize_error(self):
@asyncio.coroutine
def test():
a, b = self.create_bound_pair(zmq.DEALER, zmq.ROUTER)
msg = {
'content': {
'a': 5,
'b': 'bee',
}
}
with pytest.raises(TypeError):
yield from a.send_serialized(json, json.dumps)
yield from a.send(b'not json')
with pytest.raises(TypeError):
recvd = yield from b.recv_serialized(json.loads)
self.loop.run_until_complete(test())
示例11: test_poll
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import asyncio [as 别名]
def test_poll(self):
@asyncio.coroutine
def test():
a, b = self.create_bound_pair(zmq.PUSH, zmq.PULL)
f = b.poll(timeout=0)
yield from asyncio.sleep(0)
self.assertEqual(f.result(), 0)
f = b.poll(timeout=1)
assert not f.done()
evt = yield from f
self.assertEqual(evt, 0)
f = b.poll(timeout=1000)
assert not f.done()
yield from a.send_multipart([b'hi', b'there'])
evt = yield from f
self.assertEqual(evt, zmq.POLLIN)
recvd = yield from b.recv_multipart()
self.assertEqual(recvd, [b'hi', b'there'])
self.loop.run_until_complete(test())
示例12: test_poll_base_socket
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import asyncio [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())
示例13: can_connect
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import asyncio [as 别名]
def can_connect(self, server, client):
"""Check if client can connect to server using tcp transport"""
@asyncio.coroutine
def go():
result = False
iface = 'tcp://127.0.0.1'
port = server.bind_to_random_port(iface)
client.connect("%s:%i" % (iface, port))
msg = [b"Hello World"]
yield from server.send_multipart(msg)
if (yield from client.poll(1000)):
rcvd_msg = yield from client.recv_multipart()
self.assertEqual(rcvd_msg, msg)
result = True
return result
return self.loop.run_until_complete(go())
示例14: stop
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import asyncio [as 别名]
def stop(self):
self.active = False
self.log.warning("New block processing restricted")
self.log.warning("Stopping node connector ...")
try:
for i in self.block_loader.worker:
self.block_loader.worker[i].terminate()
except:
pass
[task.cancel() for task in self.tasks]
if self.tasks:
await asyncio.wait(self.tasks)
try:
self.zeromq_task.cancel()
await asyncio.wait([self.zeromq_task])
except:
pass
if not self.active_block.done():
self.log.warning("Waiting active block task ...")
await self.active_block
if self.rpc: await self.rpc.close()
if self.zmqContext:
self.zmqContext.destroy()
self.log.warning('Node connector terminated')
示例15: run
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import asyncio [as 别名]
def run(self):
self._logger.info("Backend started")
self._agent_socket.bind(self._agent_addr)
self._client_socket.bind(self._client_addr)
self._loop.call_later(1, self._create_safe_task, self._do_ping())
try:
while True:
socks = await self._poller.poll()
socks = dict(socks)
# New message from agent
if self._agent_socket in socks:
agent_addr, message = await ZMQUtils.recv_with_addr(self._agent_socket)
await self.handle_agent_message(agent_addr, message)
# New message from client
if self._client_socket in socks:
client_addr, message = await ZMQUtils.recv_with_addr(self._client_socket)
await self.handle_client_message(client_addr, message)
except asyncio.CancelledError:
return
except KeyboardInterrupt:
return