MCPcopy
hub / github.com/scrapy/scrapy / maybe_deferred_to_future

Function maybe_deferred_to_future

scrapy/utils/defer.py:499–524  ·  scrapy/utils/defer.py::maybe_deferred_to_future

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])

Source from the content-addressed store, hash-verified

497
498
499def 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
527def _schedule_coro(coro: Coroutine[Any, Any, Any]) -> None:

Callers 15

_scheduleMethod · 0.90
wait_forMethod · 0.90
send_catch_log_asyncFunction · 0.90
waitMethod · 0.90
run_in_threadFunction · 0.90
open_spider_asyncMethod · 0.90
close_spider_asyncMethod · 0.90
start_itemproc_asyncMethod · 0.90
scrape_func_wrappedMethod · 0.90
closeMethod · 0.90
start_asyncMethod · 0.90

Calls 2

is_asyncio_availableFunction · 0.90
deferred_to_futureFunction · 0.85

Tested by 15

process_startMethod · 0.72
test_mwman_downloadMethod · 0.72
startMethod · 0.72
get_pageMethod · 0.72
test_deferredMethod · 0.72
test_deferredMethod · 0.72