1
0
mirror of https://github.com/scrapy/scrapy.git synced 2025-02-21 04:53:19 +00:00

Remove deferred_from_coro from this PR.

This commit is contained in:
Andrey Rakhmatullin 2019-12-20 19:33:44 +05:00
parent 20289be810
commit 40697dcbfa

View File

@ -1,14 +1,10 @@
"""
Helper functions for dealing with Twisted deferreds
"""
import asyncio
import inspect
from twisted.internet import defer, task
from twisted.python import failure
from scrapy.exceptions import IgnoreRequest
from scrapy.utils.asyncio import is_asyncio_reactor_installed
def defer_fail(_failure):
@ -118,27 +114,3 @@ def iter_errback(iterable, errback, *a, **kw):
break
except Exception:
errback(failure.Failure(), *a, **kw)
def _isfuture(o):
# workaround for Python before 3.5.3 not having asyncio.isfuture
if hasattr(asyncio, 'isfuture'):
return asyncio.isfuture(o)
return isinstance(o, asyncio.Future)
def deferred_from_coro(o, asyncio_enabled=False):
"""Converts a coroutine into a Deferred, or returns the object as is if it isn't a coroutine"""
if isinstance(o, defer.Deferred):
return o
if _isfuture(o) or inspect.isawaitable(o):
if not asyncio_enabled:
# wrapping the coroutine directly into a Deferred, this doesn't work correctly with coroutines
# that use asyncio, e.g. "await asyncio.sleep(1)"
return defer.ensureDeferred(o)
else:
# wrapping the coroutine into a Future and then into a Deferred, this requires AsyncioSelectorReactor
if not is_asyncio_reactor_installed():
raise TypeError('Using coroutines requires installing AsyncioSelectorReactor')
return defer.Deferred.fromFuture(asyncio.ensure_future(o))
return o