Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _invoke_async_task(invocation, planner):
job_id = '%016x' % random.randint(0, 2**64)
context = invocation.connection.spawn_isolated_child()
_propagate_deps(invocation, planner, context)
with mitogen.core.Receiver(context.router) as started_recv:
call_recv = context.call_async(
ansible_mitogen.target.run_module_async,
job_id=job_id,
timeout_secs=invocation.timeout_secs,
started_sender=started_recv.to_sender(),
kwargs=planner.get_kwargs(),
)
# Wait for run_module_async() to crash, or for AsyncRunner to indicate
# the job file has been written.
for msg in mitogen.select.Select([started_recv, call_recv]):
if msg.receiver is call_recv:
# It can only be an exception.
raise msg.unpickle()
break
def __init__(self, econtext):
self.econtext = econtext
#: Chain ID -> CallError if prior call failed.
self._error_by_chain_id = {}
self.recv = Receiver(router=econtext.router,
handle=CALL_FUNCTION,
policy=has_parent_authority)
listen(econtext.broker, 'shutdown', self.recv.close)
to fetch the file.
:param bytes path:
FileService registered name of the input file.
:param bytes out_path:
Name of the output path on the local disk.
:returns:
Tuple of (`ok`, `metadata`), where `ok` is :data:`True` on success,
or :data:`False` if the transfer was interrupted and the output
should be discarded.
`metadata` is a dictionary of file metadata as documented in
:meth:`fetch`.
"""
LOG.debug('get_file(): fetching %r from %r', path, context)
t0 = mitogen.core.now()
recv = mitogen.core.Receiver(router=context.router)
metadata = context.call_service(
service_name=cls.name(),
method_name='fetch',
path=path,
sender=recv.to_sender(),
)
received_bytes = 0
for chunk in recv:
s = chunk.unpickle()
LOG.debug('get_file(%r): received %d bytes', path, len(s))
context.call_service_async(
service_name=cls.name(),
method_name='acknowledge',
size=len(s),
).close()
directed to a newly constructed receiver. :attr:`dst_id
` is set to the target context ID, and :attr:`reply_to
` is set to the newly constructed receiver's handle.
:param bool persist:
If :data:`False`, the handler will be unregistered after a single
message has been received.
:param mitogen.core.Message msg:
The message.
:returns:
:class:`Receiver` configured to receive any replies sent to the
message's `reply_to` handle.
"""
receiver = Receiver(self.router, persist=persist, respondent=self)
msg.dst_id = self.context_id
msg.reply_to = receiver.handle
_v and LOG.debug('sending message to %r: %r', self, msg)
self.send(msg)
return receiver
# Used later to recover hostname. A future Mitogen will provide a better
# way to get app data references back out of its IO primitives, for now you
# need to do it manually.
hostname_by_context_id = {
context.context_id: hostname
for hostname, context in contexts.items()
}
# I am a select that holds the receivers that will receive the function
# call results. Selects are one-shot by default, which means each receiver
# is removed from them as a result arrives. Therefore it means the last
# function has completed when bool(calls_sel) is False.
calls_sel = mitogen.select.Select()
# I receive the numbers as they are counted.
status_recv = mitogen.core.Receiver(router)
# Start the function calls
for hostname, context in contexts.items():
calls_sel.add(
context.call_async(
count_to,
sender=status_recv.to_sender(),
n=5,
wait=0.333
)
)
# Create a select subscribed to the function call result Select, and to the
# number-counting receiver. Any message arriving on any child of this
# Select will wake it up -- be it a message arriving on the status
# receiver, or any message arriving on any of the function call result
def MitogenServer(ch_out, netns, router):
ch_in = mitogen.core.Receiver(router)
ch_out.send(ch_in.to_sender())
trnsp_in = Transport(Channel(ch_in))
trnsp_in.file_obj.start()
trnsp_out = Transport(Channel(ch_out))
return Server(trnsp_in, trnsp_out, netns)
def close(self):
Receiver.close(self)
Sender.close(self)