PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Windows/Python3.8/WPy64-3830/WPy64-3830/python-3.8.3.amd64/Lib/asyncio/staggered.py

https://gitlab.com/abhi1tb/build
Python | 149 lines | 60 code | 8 blank | 81 comment | 17 complexity | bca378d3db917fb79e03181e278c23ad MD5 | raw file
  1. """Support for running coroutines in parallel with staggered start times."""
  2. __all__ = 'staggered_race',
  3. import contextlib
  4. import typing
  5. from . import events
  6. from . import exceptions as exceptions_mod
  7. from . import locks
  8. from . import tasks
  9. async def staggered_race(
  10. coro_fns: typing.Iterable[typing.Callable[[], typing.Awaitable]],
  11. delay: typing.Optional[float],
  12. *,
  13. loop: events.AbstractEventLoop = None,
  14. ) -> typing.Tuple[
  15. typing.Any,
  16. typing.Optional[int],
  17. typing.List[typing.Optional[Exception]]
  18. ]:
  19. """Run coroutines with staggered start times and take the first to finish.
  20. This method takes an iterable of coroutine functions. The first one is
  21. started immediately. From then on, whenever the immediately preceding one
  22. fails (raises an exception), or when *delay* seconds has passed, the next
  23. coroutine is started. This continues until one of the coroutines complete
  24. successfully, in which case all others are cancelled, or until all
  25. coroutines fail.
  26. The coroutines provided should be well-behaved in the following way:
  27. * They should only ``return`` if completed successfully.
  28. * They should always raise an exception if they did not complete
  29. successfully. In particular, if they handle cancellation, they should
  30. probably reraise, like this::
  31. try:
  32. # do work
  33. except asyncio.CancelledError:
  34. # undo partially completed work
  35. raise
  36. Args:
  37. coro_fns: an iterable of coroutine functions, i.e. callables that
  38. return a coroutine object when called. Use ``functools.partial`` or
  39. lambdas to pass arguments.
  40. delay: amount of time, in seconds, between starting coroutines. If
  41. ``None``, the coroutines will run sequentially.
  42. loop: the event loop to use.
  43. Returns:
  44. tuple *(winner_result, winner_index, exceptions)* where
  45. - *winner_result*: the result of the winning coroutine, or ``None``
  46. if no coroutines won.
  47. - *winner_index*: the index of the winning coroutine in
  48. ``coro_fns``, or ``None`` if no coroutines won. If the winning
  49. coroutine may return None on success, *winner_index* can be used
  50. to definitively determine whether any coroutine won.
  51. - *exceptions*: list of exceptions returned by the coroutines.
  52. ``len(exceptions)`` is equal to the number of coroutines actually
  53. started, and the order is the same as in ``coro_fns``. The winning
  54. coroutine's entry is ``None``.
  55. """
  56. # TODO: when we have aiter() and anext(), allow async iterables in coro_fns.
  57. loop = loop or events.get_running_loop()
  58. enum_coro_fns = enumerate(coro_fns)
  59. winner_result = None
  60. winner_index = None
  61. exceptions = []
  62. running_tasks = []
  63. async def run_one_coro(
  64. previous_failed: typing.Optional[locks.Event]) -> None:
  65. # Wait for the previous task to finish, or for delay seconds
  66. if previous_failed is not None:
  67. with contextlib.suppress(exceptions_mod.TimeoutError):
  68. # Use asyncio.wait_for() instead of asyncio.wait() here, so
  69. # that if we get cancelled at this point, Event.wait() is also
  70. # cancelled, otherwise there will be a "Task destroyed but it is
  71. # pending" later.
  72. await tasks.wait_for(previous_failed.wait(), delay)
  73. # Get the next coroutine to run
  74. try:
  75. this_index, coro_fn = next(enum_coro_fns)
  76. except StopIteration:
  77. return
  78. # Start task that will run the next coroutine
  79. this_failed = locks.Event()
  80. next_task = loop.create_task(run_one_coro(this_failed))
  81. running_tasks.append(next_task)
  82. assert len(running_tasks) == this_index + 2
  83. # Prepare place to put this coroutine's exceptions if not won
  84. exceptions.append(None)
  85. assert len(exceptions) == this_index + 1
  86. try:
  87. result = await coro_fn()
  88. except (SystemExit, KeyboardInterrupt):
  89. raise
  90. except BaseException as e:
  91. exceptions[this_index] = e
  92. this_failed.set() # Kickstart the next coroutine
  93. else:
  94. # Store winner's results
  95. nonlocal winner_index, winner_result
  96. assert winner_index is None
  97. winner_index = this_index
  98. winner_result = result
  99. # Cancel all other tasks. We take care to not cancel the current
  100. # task as well. If we do so, then since there is no `await` after
  101. # here and CancelledError are usually thrown at one, we will
  102. # encounter a curious corner case where the current task will end
  103. # up as done() == True, cancelled() == False, exception() ==
  104. # asyncio.CancelledError. This behavior is specified in
  105. # https://bugs.python.org/issue30048
  106. for i, t in enumerate(running_tasks):
  107. if i != this_index:
  108. t.cancel()
  109. first_task = loop.create_task(run_one_coro(None))
  110. running_tasks.append(first_task)
  111. try:
  112. # Wait for a growing list of tasks to all finish: poor man's version of
  113. # curio's TaskGroup or trio's nursery
  114. done_count = 0
  115. while done_count != len(running_tasks):
  116. done, _ = await tasks.wait(running_tasks)
  117. done_count = len(done)
  118. # If run_one_coro raises an unhandled exception, it's probably a
  119. # programming error, and I want to see it.
  120. if __debug__:
  121. for d in done:
  122. if d.done() and not d.cancelled() and d.exception():
  123. raise d.exception()
  124. return winner_result, winner_index, exceptions
  125. finally:
  126. # Make sure no tasks are left running if we leave this function
  127. for t in running_tasks:
  128. t.cancel()