Skip to content

Commit 41ece4b

Browse files
committed
Address feedback
1 parent 1350263 commit 41ece4b

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/apify/scrapy/_actor_runner.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,23 @@ def run_scrapy_actor(coro: Coroutine) -> None:
1515
event loops, enabling the Apify and Scrapy integration to work together.
1616
"""
1717
from scrapy.utils.reactor import install_reactor # noqa: PLC0415
18-
19-
install_reactor('twisted.internet.asyncioreactor.AsyncioSelectorReactor')
18+
from twisted.internet.error import ReactorAlreadyInstalledError # noqa: PLC0415
19+
20+
try:
21+
install_reactor('twisted.internet.asyncioreactor.AsyncioSelectorReactor')
22+
except ReactorAlreadyInstalledError:
23+
from twisted.internet import reactor as installed_reactor # noqa: PLC0415
24+
25+
reactor_cls = type(installed_reactor)
26+
if (
27+
reactor_cls.__module__ != 'twisted.internet.asyncioreactor'
28+
or reactor_cls.__name__ != 'AsyncioSelectorReactor'
29+
):
30+
raise RuntimeError(
31+
'A Twisted reactor is already installed and it is not AsyncioSelectorReactor. '
32+
'Make sure that run_scrapy_actor() is called before importing any Scrapy or Twisted '
33+
'modules that install a reactor (e.g. scrapy.crawler).'
34+
) from None
2035

2136
from twisted.internet.defer import Deferred # noqa: PLC0415
2237
from twisted.internet.task import react # noqa: PLC0415

0 commit comments

Comments
 (0)