Skip to content

Commit c4ea9f1

Browse files
authored
Prevent edge case that creates extra event loop
`asyncio.get_event_loop_policy().get_event_loop() is asyncio.get_running_loop()` is not always true, as mentioned in python/cpython#96377 (comment). The AsyncioSelectorReactor runs in the second thread and uses the already initialised event loop in the main thread to run the crawler, so a single event loop will be running code from two threads.
1 parent c8547b0 commit c4ea9f1

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

scrapy/utils/reactor.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ def _get_asyncio_event_loop():
9090

9191
def set_asyncio_event_loop(event_loop_path):
9292
"""Sets and returns the event loop with specified import path."""
93-
policy = get_asyncio_event_loop_policy()
9493
if event_loop_path is not None:
9594
event_loop_class = load_object(event_loop_path)
9695
event_loop = event_loop_class()
@@ -109,15 +108,13 @@ def set_asyncio_event_loop(event_loop_path):
109108
message="There is no current event loop",
110109
category=DeprecationWarning,
111110
)
112-
event_loop = policy.get_event_loop()
111+
event_loop = asyncio.get_event_loop()
113112
except RuntimeError:
114113
# `get_event_loop` raises RuntimeError when called with no asyncio
115114
# event loop yet installed in the following scenarios:
116-
# - From a thread other than the main thread. For example, when
117-
# using ``scrapy shell``.
118115
# - Previsibly on Python 3.14 and later.
119116
# https://github.com/python/cpython/issues/100160#issuecomment-1345581902
120-
event_loop = policy.new_event_loop()
117+
event_loop = asyncio.new_event_loop()
121118
asyncio.set_event_loop(event_loop)
122119
return event_loop
123120

0 commit comments

Comments
 (0)