本文整理匯總了Python中gevent.wait方法的典型用法代碼示例。如果您正苦於以下問題:Python gevent.wait方法的具體用法?Python gevent.wait怎麽用?Python gevent.wait使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類gevent
的用法示例。
在下文中一共展示了gevent.wait方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: wait
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import wait [as 別名]
def wait(futures, timeout=None, count=None):
"""Blocks until `count` `futures` are done (or timeout occurs) then
returns the list of done futures.
This is analogous to `gevent.wait`.
Args:
* futures (List[Future]) - The Future objects to wait for.
* timeout (None|seconds) - How long to wait for the Futures to be done.
If we hit the timeout, wait will return even if we haven't reached
`count` Futures yet.
* count (None|int) - The number of Futures to wait to be done. If None,
waits for all of them.
Returns the list of done Futures, in the order in which they were done.
"""
return list(_IWaitWrapper(futures, timeout, count))
示例2: test_main
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import wait [as 別名]
def test_main(self):
# Start an isolated task and query for its result -and finish it-
# in another task/thread
span = self.tracer.start_span('initial')
self.submit_another_task(span)
gevent.wait(timeout=5.0)
spans = self.tracer.finished_spans()
self.assertEqual(len(spans), 3)
self.assertNamesEqual(spans, ['initial', 'subtask', 'task'])
# task/subtask are part of the same trace,
# and subtask is a child of task
self.assertSameTrace(spans[1], spans[2])
self.assertIsChildOf(spans[1], spans[2])
# initial task is not related in any way to those two tasks
self.assertNotSameTrace(spans[0], spans[1])
self.assertEqual(spans[0].parent_id, None)
示例3: test_main
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import wait [as 別名]
def test_main(self):
def main_task():
with self.tracer.start_active_span('parent'):
tasks = self.submit_callbacks()
gevent.joinall(tasks)
gevent.spawn(main_task)
gevent.wait(timeout=5.0)
spans = self.tracer.finished_spans()
self.assertEquals(len(spans), 4)
self.assertNamesEqual(spans, ['task', 'task', 'task', 'parent'])
for i in range(3):
self.assertSameTrace(spans[i], spans[-1])
self.assertIsChildOf(spans[i], spans[-1])
示例4: test_main
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import wait [as 別名]
def test_main(self):
# Create a Span and use it as (explicit) parent of a pair of subtasks.
parent_span = self.tracer.start_span('parent')
self.submit_subtasks(parent_span)
gevent.wait(timeout=5.0)
# Late-finish the parent Span now.
parent_span.finish()
spans = self.tracer.finished_spans()
self.assertEqual(len(spans), 3)
self.assertNamesEqual(spans, ['task1', 'task2', 'parent'])
for i in range(2):
self.assertSameTrace(spans[i], spans[-1])
self.assertIsChildOf(spans[i], spans[-1])
self.assertTrue(spans[i].finish_time <= spans[-1].finish_time)
# Fire away a few subtasks, passing a parent Span whose lifetime
# is not tied at all to the children.
示例5: _reap_workers
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import wait [as 別名]
def _reap_workers(workers, to_close, debug_log):
"""Collects the IO workers created with _mk_workers.
After killing the workers, also closes the subprocess's open PIPE handles.
See _safe_close for caveats around closing handles on windows.
Args:
* workers (List[Greenlet]) - The IO workers to kill.
* to_close (List[...]) - (see _mk_workers for definition). The handles to
close. These originate from the `Popen.std{out,err}` handles when the
recipe engine had to use PIPEs.
* debug_log (..stream.StreamEngine.Stream)
Should not raise an exception.
"""
debug_log.write_line('reaping IO workers...')
for worker in workers:
worker.kill()
gevent.wait(workers)
debug_log.write_line(' done')
for handle_name, handle in to_close:
_safe_close(debug_log, handle_name, handle)
示例6: finish
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import wait [as 別名]
def finish(self, timeout=5):
greenlets = []
self._logs.irl.debug("Entering finish for amqp-stream monitor with %d trackers", len(self.__trackers))
for tracker in self.__trackers.values():
ttgls = tracker.start_finish(timeout=timeout)
self._logs.irl.debug(" located %s greenlets (%s) in tracker %s", len(ttgls), tracker, ttgls)
greenlets.extend(ttgls)
self._logs.irl.debug("START wait for %d greenlets (%s)", len(greenlets), greenlets)
gevent.wait(greenlets)
reses = []
self._logs.irl.debug("END wait for %d greenlets (%s)", len(greenlets), greenlets)
for gr in greenlets:
assert gr.ready(), \
'all greenlets said they completed, but this one is not {}'.format(gr)
if not gr.successful():
raise gr.exception
assert gr.successful(), \
'a greenlet {} failed with {}.'.format(gr, gr.exception)
results = gr.value
reses.append(results)
self._logs.irl.debug(" added results %s for greenlet %s", results, gr)
self._logs.irl.debug("complete set of results for finish: %s", reses)
return reses
示例7: test_main
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import wait [as 別名]
def test_main(self):
# Start a Span and let the callback-chain
# finish it when the task is done
with self.tracer.start_active_span('one', finish_on_close=False):
self.submit()
gevent.wait()
spans = self.tracer.finished_spans()
self.assertEqual(len(spans), 1)
self.assertEqual(spans[0].operation_name, 'one')
for i in range(1, 4):
self.assertEqual(spans[0].tags.get('key%s' % i, None), str(i))
示例8: test
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import wait [as 別名]
def test(self):
client = Client(self.tracer, self.queue)
gevent.spawn(self.server.run)
gevent.spawn(client.send)
gevent.wait(timeout=5.0)
spans = self.tracer.finished_spans()
self.assertIsNotNone(get_one_by_tag(spans,
tags.SPAN_KIND,
tags.SPAN_KIND_RPC_SERVER))
self.assertIsNotNone(get_one_by_tag(spans,
tags.SPAN_KIND,
tags.SPAN_KIND_RPC_CLIENT))
示例9: result
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import wait [as 別名]
def result(self, timeout=None):
"""Blocks until this Future is done, then returns its value, or raises
its exception.
Args:
* timeout (None|seconds) - How long to wait for the Future to be done.
Returns the result if the Future is done.
Raises the Future's exception, if the Future is done with an error.
Raises Timeout if the Future is not done within the given timeout.
"""
with gevent.Timeout(timeout, exception=FuturesApi.Timeout()):
return self._greenlet.get()
示例10: exception
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import wait [as 別名]
def exception(self, timeout=None):
"""Blocks until this Future is done, then returns (not raises) this
Future's exception (or None if there was no exception).
Args:
* timeout (None|seconds) - How long to wait for the Future to be done.
Returns the exception instance which would be raised from `result` if
the Future is Done, otherwise None.
Raises Timeout if the Future is not done within the given timeout.
"""
with gevent.Timeout(timeout, exception=FuturesApi.Timeout()):
done = gevent.wait([self._greenlet])[0]
return done.exception
示例11: _kill
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import wait [as 別名]
def _kill(proc, gid):
"""Kills the process as gracefully as possible:
* Send CTRL_BREAK_EVENT (to the process group, there's no other way)
* Give main process 30s to quit.
* Call TerminateProcess on the process we spawned.
Unfortunately, we don't have a mechanism to do 'TerminateProcess' to the
process's group, so this could leak processes on Windows.
Returns the process's returncode.
"""
# TODO(iannucci): Use a Job Object for process management. Use subprocess42
# for reference.
del gid # gid is not supported on Windows
try:
# pylint: disable=no-member
proc.send_signal(signal.CTRL_BREAK_EVENT)
except OSError:
pass
# TODO(iannucci): This API changes in python3 to raise an exception on
# timeout.
proc.wait(timeout=30)
if proc.returncode is not None:
return proc.returncode
try:
proc.terminate()
except OSError:
pass
return proc.wait()
示例12: run_local_process
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import wait [as 別名]
def run_local_process(
command,
stdin=None,
timeout=None,
print_output=False,
print_prefix=None,
):
process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
if stdin:
write_stdin(stdin, process.stdin)
combined_output = read_buffers_into_queue(
process.stdout,
process.stderr,
timeout=timeout,
print_output=print_output,
print_prefix=print_prefix,
)
logger.debug('--> Waiting for exit status...')
process.wait()
logger.debug('--> Command exit status: {0}'.format(process.returncode))
# Close any open file descriptors
process.stdout.close()
process.stderr.close()
return process.returncode, combined_output
示例13: read_buffers_into_queue
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import wait [as 別名]
def read_buffers_into_queue(
stdout_buffer, stderr_buffer, timeout, print_output, print_prefix,
):
output_queue = Queue()
# Iterate through outputs to get an exit status and generate desired list
# output, done in two greenlets so stdout isn't printed before stderr. Not
# attached to state.pool to avoid blocking it with 2x n-hosts greenlets.
stdout_reader = gevent.spawn(
read_buffer,
'stdout',
stdout_buffer,
output_queue,
print_output=print_output,
print_func=lambda line: '{0}{1}'.format(print_prefix, line),
)
stderr_reader = gevent.spawn(
read_buffer,
'stderr',
stderr_buffer,
output_queue,
print_output=print_output,
print_func=lambda line: '{0}{1}'.format(
print_prefix, click.style(line, 'red'),
),
)
# Wait on output, with our timeout (or None)
greenlets = gevent.wait((stdout_reader, stderr_reader), timeout=timeout)
# Timeout doesn't raise an exception, but gevent.wait returns the greenlets
# which did complete. So if both haven't completed, we kill them and fail
# with a timeout.
if len(greenlets) != 2:
stdout_reader.kill()
stderr_reader.kill()
raise timeout_error()
return list(output_queue.queue)
示例14: sockets
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import wait [as 別名]
def sockets(redis):
from dallinger.experiment_server import sockets
sockets.redis_conn = redis
# use a separate ChatBackend for each test
sockets.chat_backend = sockets.ChatBackend()
yield sockets
# make sure all greenlets complete
gevent.wait()
示例15: test_subscribes_to_redis
# 需要導入模塊: import gevent [as 別名]
# 或者: from gevent import wait [as 別名]
def test_subscribes_to_redis(self, sockets, pubsub):
sockets.Channel("custom").start()
gevent.wait()
pubsub.subscribe.assert_called_once_with([b"custom"])