MCPcopy
hub / github.com/scrapy/scrapy / deferred_to_future

Function deferred_to_future

scrapy/utils/defer.py:469–496  ·  view source on GitHub ↗

Return an :class:`asyncio.Future` object that wraps *d*. This function requires :class:`~twisted.internet.asyncioreactor.AsyncioSelectorReactor` to be installed. When :ref:`using the asyncio reactor `, you cannot await on :class:`~twisted.internet.defer.Deferre

(d: Deferred[_T])

Source from the content-addressed store, hash-verified

467
468
469def deferred_to_future(d: Deferred[_T]) -> Future[_T]:
470 """Return an :class:`asyncio.Future` object that wraps *d*.
471
472 This function requires
473 :class:`~twisted.internet.asyncioreactor.AsyncioSelectorReactor` to be
474 installed.
475
476 When :ref:`using the asyncio reactor <install-asyncio>`, you cannot await
477 on :class:`~twisted.internet.defer.Deferred` objects from :ref:`Scrapy
478 callables defined as coroutines <coroutine-support>`, you can only await on
479 ``Future`` objects. Wrapping ``Deferred`` objects into ``Future`` objects
480 allows you to wait on them::
481
482 class MySpider(Spider):
483 ...
484 async def parse(self, response):
485 additional_request = scrapy.Request('https://example.org/price')
486 deferred = self.crawler.engine.download(additional_request)
487 additional_response = await deferred_to_future(deferred)
488
489 .. versionchanged:: 2.14
490 This function no longer installs an asyncio loop if called before the
491 Twisted asyncio reactor is installed. A :exc:`RuntimeError` is raised
492 in this case.
493 """
494 if not is_asyncio_available():
495 raise RuntimeError("deferred_to_future() requires AsyncioSelectorReactor.")
496 return d.asFuture(asyncio.get_event_loop())
497
498
499def maybe_deferred_to_future(d: Deferred[_T]) -> Deferred[_T] | Future[_T]:

Callers 7

test_deferredMethod · 0.90
parseMethod · 0.90
process_itemMethod · 0.90
wrapper_coroFunction · 0.90
maybe_deferred_to_futureFunction · 0.85

Calls 1

is_asyncio_availableFunction · 0.90

Tested by 4

test_deferredMethod · 0.72
process_itemMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…