Return *d* as an object that can be awaited from a :ref:`Scrapy callable defined as a coroutine <coroutine-support>`. What you can await in Scrapy callables defined as coroutines depends on the value of :setting:`TWISTED_REACTOR`: - When :ref:`using the asyncio reactor <install-a
(d: Deferred[_T])
| 497 | |
| 498 | |
| 499 | def maybe_deferred_to_future(d: Deferred[_T]) -> Deferred[_T] | Future[_T]: |
| 500 | class="st">"""Return *d* as an object that can be awaited from a :ref:`Scrapy callable |
| 501 | defined as a coroutine <coroutine-support>`. |
| 502 | |
| 503 | What you can await in Scrapy callables defined as coroutines depends on the |
| 504 | value of :setting:`TWISTED_REACTOR`: |
| 505 | |
| 506 | - When :ref:`using the asyncio reactor <install-asyncio>`, you can only |
| 507 | await on :class:`asyncio.Future` objects. |
| 508 | |
| 509 | - When not using the asyncio reactor, you can only await on |
| 510 | :class:`~twisted.internet.defer.Deferred` objects. |
| 511 | |
| 512 | If you want to write code that uses ``Deferred`` objects but works with any |
| 513 | reactor, use this function on all ``Deferred`` objects:: |
| 514 | |
| 515 | class MySpider(Spider): |
| 516 | ... |
| 517 | async def parse(self, response): |
| 518 | additional_request = scrapy.Request(&class="cm">#x27;https://example.org/price') |
| 519 | deferred = self.crawler.engine.download(additional_request) |
| 520 | additional_response = await maybe_deferred_to_future(deferred) |
| 521 | class="st">""" |
| 522 | if not is_asyncio_available(): |
| 523 | return d |
| 524 | return deferred_to_future(d) |
| 525 | |
| 526 | |
| 527 | def _schedule_coro(coro: Coroutine[Any, Any, Any]) -> None: |