MCPcopy
hub / github.com/scrapy/scrapy / set_asyncio_event_loop

Function set_asyncio_event_loop

scrapy/utils/reactor.py:134–164  ·  view source on GitHub ↗

Sets and returns the event loop with specified import path.

(event_loop_path: str | None)

Source from the content-addressed store, hash-verified

132
133
134def set_asyncio_event_loop(event_loop_path: str | None) -> AbstractEventLoop:
135 """Sets and returns the event loop with specified import path."""
136 if event_loop_path is not None:
137 event_loop_class: type[AbstractEventLoop] = load_object(event_loop_path)
138 event_loop = _get_asyncio_event_loop()
139 if not isinstance(event_loop, event_loop_class):
140 event_loop = event_loop_class()
141 asyncio.set_event_loop(event_loop)
142 else:
143 try:
144 with catch_warnings():
145 # In Python 3.10.9, 3.11.1, 3.12 and 3.13, a DeprecationWarning
146 # is emitted about the lack of a current event loop, because in
147 # Python 3.14 and later `get_event_loop` will raise a
148 # RuntimeError in that event. Because our code is already
149 # prepared for that future behavior, we ignore the deprecation
150 # warning.
151 filterwarnings(
152 "ignore",
153 message="There is no current event loop",
154 category=DeprecationWarning,
155 )
156 event_loop = asyncio.get_event_loop()
157 except RuntimeError:
158 # `get_event_loop` raises RuntimeError when called with no asyncio
159 # event loop yet installed in the following scenarios:
160 # - Previsibly on Python 3.14 and later.
161 # https://github.com/python/cpython/issues/100160#issuecomment-1345581902
162 event_loop = asyncio.new_event_loop()
163 asyncio.set_event_loop(event_loop)
164 return event_loop
165
166
167def verify_installed_reactor(reactor_path: str) -> None:

Callers 5

__init__Method · 0.90
_scheduleMethod · 0.90
install_reactorFunction · 0.85
_get_asyncio_event_loopFunction · 0.85

Calls 2

load_objectFunction · 0.90
_get_asyncio_event_loopFunction · 0.85

Tested by 1