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])
| 467 | |
| 468 | |
| 469 | def 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 | |
| 499 | def maybe_deferred_to_future(d: Deferred[_T]) -> Deferred[_T] | Future[_T]: |
searching dependent graphs…