MCPcopy
hub / github.com/scrapy/scrapy / is_asyncio_available

Function is_asyncio_available

scrapy/utils/asyncio.py:34–92  ·  scrapy/utils/asyncio.py::is_asyncio_available

Check if it's possible to call asyncio code that relies on the asyncio event loop. .. versionadded:: 2.14 This function returns ``True`` if there is a running asyncio event loop. If there is no such loop, it returns ``True`` if the Twisted reactor that is installed is :class:`~

()

Source from the content-addressed store, hash-verified

32
33
34def is_asyncio_available() -> bool:
35 class="st">"""Check if it&class="cm">#x27;s possible to call asyncio code that relies on the asyncio event loop.
36
37 .. versionadded:: 2.14
38
39 This function returns ``True`` if there is a running asyncio event loop. If
40 there is no such loop, it returns ``True`` if the Twisted reactor that is
41 installed is
42 :class:`~twisted.internet.asyncioreactor.AsyncioSelectorReactor`, returns
43 ``False`` if a different reactor is installed, and raises a
44 :exc:`RuntimeError` if no reactor is installed.
45
46 Code that doesn&class="cm">#x27;t directly require a Twisted reactor should use this
47 function while code that requires
48 :class:`~twisted.internet.asyncioreactor.AsyncioSelectorReactor` should use
49 :func:`~scrapy.utils.reactor.is_asyncio_reactor_installed`.
50
51 When this returns ``True``, an asyncio loop is installed and used by
52 Scrapy. It&class="cm">#x27;s possible to call functions that require it, such as
53 :func:`asyncio.sleep`, and await on :class:`asyncio.Future` objects in
54 Scrapy-related code.
55
56 When this returns ``False``, a non-asyncio Twisted reactor is installed.
57 It&class="cm">#x27;s not possible to use asyncio features that require an asyncio event
58 loop or await on :class:`asyncio.Future` objects in Scrapy-related code,
59 but it&class="cm">#x27;s possible to await on :class:`~twisted.internet.defer.Deferred`
60 objects.
61
62 .. note:: As this function uses :func:`asyncio.get_running_loop()`, it will
63 only detect the event loop if called in the same thread and from the
64 code that runs inside that loop (this shouldn&class="cm">#x27;t be a problem when
65 calling it from code such as spiders and Scrapy components, if Scrapy
66 is run using one of the supported ways).
67
68 .. versionchanged:: 2.15.0
69 This function now also returns ``True`` if there is a running asyncio
70 loop, even if no Twisted reactor is installed.
71 class="st">"""
72
73 class="cm"># Check if there is a running asyncio loop.
74 class="cm"># Can't easily check for an installed but not running one, and if we
75 class="cm"># checked that there could be false positives due to some 3rd-party code
76 class="cm"># installing it as a side effect (e.g. by calling get_event_loop()).
77 try:
78 asyncio.get_running_loop()
79 except RuntimeError:
80 pass
81 else:
82 return True
83
84 class="cm"># Check if there is an installed asyncio reactor (it doesn't need to be
85 class="cm"># running).
86 if not is_reactor_installed():
87 raise RuntimeError(
88 class="st">"is_asyncio_available() called without an installed reactor"
89 class="st">" or running asyncio loop."
90 )
91

Callers 15

send_catch_log_asyncFunction · 0.90
is_reactorlessFunction · 0.90
_defer_sleep_asyncFunction · 0.90
deferred_from_coroFunction · 0.90
deferred_to_futureFunction · 0.90
maybe_deferred_to_futureFunction · 0.90
_schedule_coroFunction · 0.90
start_asyncMethod · 0.90
stop_asyncMethod · 0.90
__init__Method · 0.90
process_itemMethod · 0.90

Calls 2

is_reactor_installedFunction · 0.90

Tested by 1