Skip to content

Commit 59e16e3

Browse files
committed
Fix disk quota middleware on non-asyncio reactor
1 parent 91f34db commit 59e16e3

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

sh_scrapy/diskquota.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from scrapy.crawler import Crawler
1212
from scrapy.exceptions import NotConfigured
1313
from scrapy.http import Request, Response
14+
from scrapy.utils.defer import deferred_from_coro
1415

1516
from sh_scrapy import _SCRAPY_NO_SPIDER_ARG
1617

@@ -57,10 +58,15 @@ def __init__(self, crawler: Crawler):
5758

5859
def process_spider_exception(self, response: Response, exception: Exception) -> None:
5960
if self._is_disk_quota_error(exception):
61+
from scrapy.utils.asyncio import is_asyncio_available
62+
6063
coro = self.crawler.engine.close_spider_async(reason="diskusage_exceeded")
61-
task = asyncio.create_task(coro)
62-
self._tasks.add(task)
63-
task.add_done_callback(self._tasks.discard)
64+
if is_asyncio_available():
65+
task = asyncio.create_task(coro)
66+
self._tasks.add(task)
67+
task.add_done_callback(self._tasks.discard)
68+
else:
69+
deferred_from_coro(coro)
6470

6571
else:
6672

0 commit comments

Comments
 (0)