runners.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. __all__ = ('Runner', 'run')
  2. import contextvars
  3. import enum
  4. import functools
  5. import threading
  6. import signal
  7. import sys
  8. from . import coroutines
  9. from . import events
  10. from . import exceptions
  11. from . import tasks
  12. class _State(enum.Enum):
  13. CREATED = "created"
  14. INITIALIZED = "initialized"
  15. CLOSED = "closed"
  16. class Runner:
  17. """A context manager that controls event loop life cycle.
  18. The context manager always creates a new event loop,
  19. allows to run async functions inside it,
  20. and properly finalizes the loop at the context manager exit.
  21. If debug is True, the event loop will be run in debug mode.
  22. If loop_factory is passed, it is used for new event loop creation.
  23. asyncio.run(main(), debug=True)
  24. is a shortcut for
  25. with asyncio.Runner(debug=True) as runner:
  26. runner.run(main())
  27. The run() method can be called multiple times within the runner's context.
  28. This can be useful for interactive console (e.g. IPython),
  29. unittest runners, console tools, -- everywhere when async code
  30. is called from existing sync framework and where the preferred single
  31. asyncio.run() call doesn't work.
  32. """
  33. # Note: the class is final, it is not intended for inheritance.
  34. def __init__(self, *, debug=None, loop_factory=None):
  35. self._state = _State.CREATED
  36. self._debug = debug
  37. self._loop_factory = loop_factory
  38. self._loop = None
  39. self._context = None
  40. self._interrupt_count = 0
  41. self._set_event_loop = False
  42. def __enter__(self):
  43. self._lazy_init()
  44. return self
  45. def __exit__(self, exc_type, exc_val, exc_tb):
  46. self.close()
  47. def close(self):
  48. """Shutdown and close event loop."""
  49. if self._state is not _State.INITIALIZED:
  50. return
  51. try:
  52. loop = self._loop
  53. _cancel_all_tasks(loop)
  54. loop.run_until_complete(loop.shutdown_asyncgens())
  55. loop.run_until_complete(loop.shutdown_default_executor())
  56. finally:
  57. if self._set_event_loop:
  58. events.set_event_loop(None)
  59. loop.close()
  60. self._loop = None
  61. self._state = _State.CLOSED
  62. def get_loop(self):
  63. """Return embedded event loop."""
  64. self._lazy_init()
  65. return self._loop
  66. def run(self, coro, *, context=None):
  67. """Run a coroutine inside the embedded event loop."""
  68. if not coroutines.iscoroutine(coro):
  69. raise ValueError("a coroutine was expected, got {!r}".format(coro))
  70. if events._get_running_loop() is not None:
  71. # fail fast with short traceback
  72. raise RuntimeError(
  73. "Runner.run() cannot be called from a running event loop")
  74. self._lazy_init()
  75. if context is None:
  76. context = self._context
  77. task = self._loop.create_task(coro, context=context)
  78. if (threading.current_thread() is threading.main_thread()
  79. and signal.getsignal(signal.SIGINT) is signal.default_int_handler
  80. ):
  81. sigint_handler = functools.partial(self._on_sigint, main_task=task)
  82. try:
  83. signal.signal(signal.SIGINT, sigint_handler)
  84. except ValueError:
  85. # `signal.signal` may throw if `threading.main_thread` does
  86. # not support signals (e.g. embedded interpreter with signals
  87. # not registered - see gh-91880)
  88. sigint_handler = None
  89. else:
  90. sigint_handler = None
  91. self._interrupt_count = 0
  92. try:
  93. return self._loop.run_until_complete(task)
  94. except exceptions.CancelledError:
  95. if self._interrupt_count > 0:
  96. uncancel = getattr(task, "uncancel", None)
  97. if uncancel is not None and uncancel() == 0:
  98. raise KeyboardInterrupt()
  99. raise # CancelledError
  100. finally:
  101. if (sigint_handler is not None
  102. and signal.getsignal(signal.SIGINT) is sigint_handler
  103. ):
  104. signal.signal(signal.SIGINT, signal.default_int_handler)
  105. def _lazy_init(self):
  106. if self._state is _State.CLOSED:
  107. raise RuntimeError("Runner is closed")
  108. if self._state is _State.INITIALIZED:
  109. return
  110. if self._loop_factory is None:
  111. self._loop = events.new_event_loop()
  112. if not self._set_event_loop:
  113. # Call set_event_loop only once to avoid calling
  114. # attach_loop multiple times on child watchers
  115. events.set_event_loop(self._loop)
  116. self._set_event_loop = True
  117. else:
  118. self._loop = self._loop_factory()
  119. if self._debug is not None:
  120. self._loop.set_debug(self._debug)
  121. self._context = contextvars.copy_context()
  122. self._state = _State.INITIALIZED
  123. def _on_sigint(self, signum, frame, main_task):
  124. self._interrupt_count += 1
  125. if self._interrupt_count == 1 and not main_task.done():
  126. main_task.cancel()
  127. # wakeup loop if it is blocked by select() with long timeout
  128. self._loop.call_soon_threadsafe(lambda: None)
  129. return
  130. raise KeyboardInterrupt()
  131. def run(main, *, debug=None):
  132. """Execute the coroutine and return the result.
  133. This function runs the passed coroutine, taking care of
  134. managing the asyncio event loop and finalizing asynchronous
  135. generators.
  136. This function cannot be called when another asyncio event loop is
  137. running in the same thread.
  138. If debug is True, the event loop will be run in debug mode.
  139. This function always creates a new event loop and closes it at the end.
  140. It should be used as a main entry point for asyncio programs, and should
  141. ideally only be called once.
  142. Example:
  143. async def main():
  144. await asyncio.sleep(1)
  145. print('hello')
  146. asyncio.run(main())
  147. """
  148. if events._get_running_loop() is not None:
  149. # fail fast with short traceback
  150. raise RuntimeError(
  151. "asyncio.run() cannot be called from a running event loop")
  152. with Runner(debug=debug) as runner:
  153. return runner.run(main)
  154. def _cancel_all_tasks(loop):
  155. to_cancel = tasks.all_tasks(loop)
  156. if not to_cancel:
  157. return
  158. for task in to_cancel:
  159. task.cancel()
  160. loop.run_until_complete(tasks.gather(*to_cancel, return_exceptions=True))
  161. for task in to_cancel:
  162. if task.cancelled():
  163. continue
  164. if task.exception() is not None:
  165. loop.call_exception_handler({
  166. 'message': 'unhandled exception during asyncio.run() shutdown',
  167. 'exception': task.exception(),
  168. 'task': task,
  169. })