MCPcopy Index your code
hub / github.com/python/cpython / run

Method run

Lib/asyncio/runners.py:87–138  ·  view source on GitHub ↗

Run code in the embedded event loop.

(self, coro, *, context=None)

Source from the content-addressed store, hash-verified

85 return self._loop
86
87 def run(self, coro, *, context=None):
88 """Run code in the embedded event loop."""
89 if events._get_running_loop() is not None:
90 # fail fast with short traceback
91 raise RuntimeError(
92 "Runner.run() cannot be called from a running event loop")
93
94 self._lazy_init()
95
96 if not coroutines.iscoroutine(coro):
97 if inspect.isawaitable(coro):
98 async def _wrap_awaitable(awaitable):
99 return await awaitable
100
101 coro = _wrap_awaitable(coro)
102 else:
103 raise TypeError('An asyncio.Future, a coroutine or an '
104 'awaitable is required')
105
106 if context is None:
107 context = self._context
108
109 task = self._loop.create_task(coro, context=context)
110
111 if (threading.current_thread() is threading.main_thread()
112 and signal.getsignal(signal.SIGINT) is signal.default_int_handler
113 ):
114 sigint_handler = functools.partial(self._on_sigint, main_task=task)
115 try:
116 signal.signal(signal.SIGINT, sigint_handler)
117 except ValueError:
118 # `signal.signal` may throw if `threading.main_thread` does
119 # not support signals (e.g. embedded interpreter with signals
120 # not registered - see gh-91880)
121 sigint_handler = None
122 else:
123 sigint_handler = None
124
125 self._interrupt_count = 0
126 try:
127 return self._loop.run_until_complete(task)
128 except exceptions.CancelledError:
129 if self._interrupt_count > 0:
130 uncancel = getattr(task, "uncancel", None)
131 if uncancel is not None and uncancel() == 0:
132 raise KeyboardInterrupt()
133 raise # CancelledError
134 finally:
135 if (sigint_handler is not None
136 and signal.getsignal(signal.SIGINT) is sigint_handler
137 ):
138 signal.signal(signal.SIGINT, signal.default_int_handler)
139
140 def _lazy_init(self):
141 if self._state is _State.CLOSED:

Callers 7

_runMethod · 0.45
__eager_startMethod · 0.45
runFunction · 0.45

Calls 5

_lazy_initMethod · 0.95
_wrap_awaitableFunction · 0.85
partialMethod · 0.80
create_taskMethod · 0.45
run_until_completeMethod · 0.45