本文整理汇总了Python中zmq.Again方法的典型用法代码示例。如果您正苦于以下问题:Python zmq.Again方法的具体用法?Python zmq.Again怎么用?Python zmq.Again使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zmq
的用法示例。
在下文中一共展示了zmq.Again方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: backend_status_datasets
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Again [as 别名]
def backend_status_datasets() -> BackendStatusDatasetsSchema:
"""
Returns a combined list of datasets from all agents.
Caveat: In case of collision of dataset ids when there are multiple agents,
this API will only return one dataset per colliding ID. Collision is
extremally unlikely though and it shouldn't be a problem in the real world.
This endpoint is not stable and may be subject to change in the future.
"""
datasets: Dict[str, int] = {}
for agent_spec in db.get_active_agents().values():
try:
ursa = UrsaDb(agent_spec.ursadb_url)
datasets.update(ursa.topology()["result"]["datasets"])
except Again:
pass
return BackendStatusDatasetsSchema(datasets=datasets)
示例2: _transmit_one_msg_throughlistener
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Again [as 别名]
def _transmit_one_msg_throughlistener(self, msg, ident) -> Tuple[bool, Optional[str], bool]:
def prepare_error_msg(ex):
err_str = '{} got error {} while sending through listener to {}' \
.format(self, ex, ident)
print(err_str)
return err_str
need_to_resend = False
if isinstance(ident, str):
ident = ident.encode()
try:
msg = self._prepare_to_send(msg)
self.listener.send_multipart([ident, msg], flags=zmq.NOBLOCK)
except zmq.Again as ex:
need_to_resend = True
return False, prepare_error_msg(ex), need_to_resend
except zmq.ZMQError as ex:
need_to_resend = (ex.errno == 113)
return False, prepare_error_msg(ex), need_to_resend
except Exception as ex:
return False, prepare_error_msg(ex), need_to_resend
return True, None, need_to_resend
示例3: _receiveFromListener
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Again [as 别名]
def _receiveFromListener(self, quota: Quota) -> int:
"""
Receives messages from listener
:param quota: number of messages to receive
:return: number of received messages
"""
i = 0
incoming_size = 0
try:
ident, msg = self.listener.recv_multipart(flags=zmq.NOBLOCK)
if msg:
# Router probing sends empty message on connection
incoming_size += len(msg)
i += 1
self._verifyAndAppend(msg, ident)
except zmq.Again as e:
return i
except zmq.ZMQError as e:
print("Strange ZMQ behaviour during node-to-node message receiving, experienced {}".format(e))
if i > 0:
print('{} got {} messages through listener'.
format(self, i))
return i
示例4: transmit
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Again [as 别名]
def transmit(self, msg, uid, timeout=None, serialized=False, is_batch=False):
remote = self.remotes.get(uid)
err_str = None
if not remote:
return False, err_str
socket = remote.socket
if not socket:
return False, err_str
try:
if not serialized:
msg = self.prepare_to_send(msg)
print('{} transmitting message {} to {} by socket {} {}'
.format(self, msg, uid, socket.FD, socket.underlying))
socket.send(msg, flags=zmq.NOBLOCK)
return True, err_str
except zmq.Again:
print('{} could not transmit message to {}'.format(self, uid))
return False, err_str
示例5: recv_array
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Again [as 别名]
def recv_array(socket, flags=0, copy=False, track=False, block=True):
"""recv a numpy array"""
if block:
md = socket.recv_json(flags=flags)
msg = socket.recv(flags=flags, copy=copy, track=track)
buf = bytearray(msg)
A = np.frombuffer(buf, dtype=md['dtype'])
return A.reshape(md['shape'])
else:
try:
md = socket.recv_json(flags=flags | zmq.NOBLOCK)
msg = socket.recv(flags=flags | zmq.NOBLOCK, copy=copy, track=track)
buf = bytearray(msg)
A = np.frombuffer(buf, dtype=md['dtype'])
return A.reshape(md['shape'])
except zmq.Again:
return False
示例6: test_disconnection
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Again [as 别名]
def test_disconnection(self):
""" Test the disconnection of subscribers. """
from supvisors.utils import InternalEventHeaders
# get the local address
local_address = self.supvisors.address_mapper.local_address
# test remote disconnection
address = next(address
for address in self.supvisors.address_mapper.addresses
if address != local_address)
self.subscriber.disconnect([address])
# send a tick event from the local publisher
payload = {'date': 1000}
self.publisher.send_tick_event(payload)
# check the reception of the tick event
msg = self.receive('Tick')
self.assertTupleEqual((InternalEventHeaders.TICK,
local_address, payload), msg)
# test local disconnection
self.subscriber.disconnect([local_address])
# send a tick event from the local publisher
self.publisher.send_tick_event(payload)
# check the non-reception of the tick event
with self.assertRaises(zmq.Again):
self.subscriber.receive()
示例7: generator_from_zmq_pull
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Again [as 别名]
def generator_from_zmq_pull(context, host):
socket = context.socket(zmq.PULL)
# TODO: Configure socket with clean properties to avoid message overload.
if host.endswith('/'):
host = host[:-1]
print_item("+", "Binding ZMQ pull socket : " + colorama.Fore.CYAN + "{0}".format(host) + colorama.Style.RESET_ALL)
socket.bind(host)
while True:
try:
message = socket.recv(flags=zmq.NOBLOCK)
except zmq.Again as e:
message = None
if message is None:
yield None # NOTE: We have to make the generator non blocking.
else:
task = json.loads(message)
yield task
示例8: recv_params_from_learner
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Again [as 别名]
def recv_params_from_learner(self):
"""Get new params and sync. return True if success, False otherwise."""
received = False
try:
new_params_id = self.sub_socket.recv(zmq.DONTWAIT)
received = True
except zmq.Again:
# Although learner doesn't send params, don't wait
pass
if received:
new_param_info = pa.deserialize(new_params_id)
update_step, new_params = new_param_info
self.update_step = update_step
self.worker.synchronize(new_params)
# Add new entry for scores dict
self.scores[self.update_step] = []
示例9: recv_worker_data
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Again [as 别名]
def recv_worker_data(self):
"""Receive replay data from worker and incorporate to buffer."""
received = False
try:
new_replay_data_id = self.pull_socket.recv(zmq.DONTWAIT)
received = True
except zmq.Again:
pass
if received:
new_replay_data = pa.deserialize(new_replay_data_id)
experience, priorities = new_replay_data
for idx in range(len(experience["states"])):
transition = (
experience["states"][idx],
experience["actions"][idx],
experience["rewards"][idx],
experience["next_states"][idx],
experience["dones"][idx],
)
self.buffer.add(transition)
self.buffer.update_priorities([len(self.buffer) - 1], priorities[idx])
示例10: receive_replies
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Again [as 别名]
def receive_replies(self, context=None):
result_sock = context.socket(zmq.PULL)
self.receive_port = result_sock.bind_to_random_port("tcp://*")
logger.debug("Result port is %s" % self.receive_port)
set_timeouts_on_socket(result_sock)
while True:
try:
message_reply = result_sock.recv_json()
if message_reply['status'] == 'success':
self.success(message_reply['id'])
elif message_reply['status'] == 'fail':
self.failed(message_reply['id'])
else:
logger.warn("Received unknown status feedback %s" % message_reply['status'])
except zmq.Again:
time.sleep(1)
示例11: run
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Again [as 别名]
def run(self):
if self.should_stop: # early exit
return 1
context = zmq.Context()
socket = context.socket(zmq.SUB)
logger.info(f"subscribing to events from {self.socket_uri}…")
socket.connect(self.socket_uri)
for event in self.events:
logger.debug(f".. {event}")
socket.setsockopt_string(zmq.SUBSCRIBE, event)
while not self.should_stop:
try:
received_string = socket.recv_string(zmq.DONTWAIT)
self.handle_broadcast_event(received_string)
except zmq.Again:
pass
if self.should_poll:
self.sync_tasks_and_containers()
self.poll()
else:
self.sleep()
示例12: receive_message
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Again [as 别名]
def receive_message(socket, blocking=True):
flags = 0 if blocking else zmq.NOBLOCK
try:
cmd, data = socket.recv_multipart(flags=flags)
return cmd, data
except zmq.Again:
return None, None
except zmq.ContextTerminated:
print("Context terminated ..")
return None, None
except KeyboardInterrupt:
return None, None
示例13: backend_status
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Again [as 别名]
def backend_status() -> BackendStatusSchema:
"""
Returns the current status of backend services, and returns it. Intended to
be used by the webpage.
This endpoint is not stable and may be subject to change in the future.
"""
agents = []
components = {
"mquery": mquery_version(),
}
for name, agent_spec in db.get_active_agents().items():
try:
ursa = UrsaDb(agent_spec.ursadb_url)
status = ursa.status()
tasks = status["result"]["tasks"]
ursadb_version = status["result"]["ursadb_version"]
agents.append(
AgentSchema(
name=name, alive=True, tasks=tasks, spec=agent_spec
)
)
components[f"ursadb ({name})"] = ursadb_version
except Again:
agents.append(
AgentSchema(name=name, alive=False, tasks=[], spec=agent_spec)
)
components[f"ursadb ({name})"] = "unknown"
return BackendStatusSchema(agents=agents, components=components,)
示例14: recv_all
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Again [as 别名]
def recv_all(self, socket, flags=0, validate=True):
'''Receive all messages currently available from the given socket.'''
messages = []
while True:
try:
messages.append(self.recv_message(socket, flags | zmq.NOBLOCK, validate))
except zmq.Again:
return messages
示例15: _add_timeout
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import Again [as 别名]
def _add_timeout(self, future, timeout):
"""Add a timeout for a send or recv Future"""
def future_timeout():
if future.done():
# future already resolved, do nothing
return
# raise EAGAIN
future.set_exception(_zmq.Again())
self._call_later(timeout, future_timeout)