diff --git a/pyproject.toml b/pyproject.toml index 14cafefaa2..ffe634cac5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,7 +34,7 @@ keywords = [ "scraping", ] dependencies = [ - "async-timeout>=5.0.1", + "async-timeout>=5.0.1; python_full_version < '3.11'", "cachetools>=5.5.0", "colorama>=0.4.0", "impit>=0.13.2", diff --git a/src/crawlee/_utils/time.py b/src/crawlee/_utils/time.py index 10d6fd3d8d..c6042916a7 100644 --- a/src/crawlee/_utils/time.py +++ b/src/crawlee/_utils/time.py @@ -1,17 +1,25 @@ from __future__ import annotations +import asyncio +import sys import time from contextlib import contextmanager from dataclasses import dataclass from datetime import timedelta from typing import TYPE_CHECKING -from async_timeout import Timeout, timeout - if TYPE_CHECKING: from collections.abc import Iterator from types import TracebackType +if sys.version_info >= (3, 11): + timeout_ctx = asyncio.timeout +else: + # TODO: `async-timeout` backports `asyncio.timeout` for Python 3.10; drop once the + # project floor bump to 3.11 + # https://github.com/apify/crawlee-python/issues/2052 + from async_timeout import timeout as timeout_ctx # type: ignore[no-redef] + _SECONDS_PER_MINUTE = 60 _SECONDS_PER_HOUR = 3600 @@ -46,7 +54,7 @@ class SharedTimeout: def __init__(self, timeout: timedelta) -> None: self._remaining_timeout = timeout - self._active_timeout: Timeout | None = None + self._active_timeout: asyncio.Timeout | None = None self._activation_timestamp: float | None = None async def __aenter__(self) -> timedelta: @@ -54,7 +62,7 @@ async def __aenter__(self) -> timedelta: raise RuntimeError('A shared timeout context cannot be entered twice at the same time') self._activation_timestamp = time.monotonic() - self._active_timeout = new_timeout = timeout(self._remaining_timeout.total_seconds()) + self._active_timeout = new_timeout = timeout_ctx(self._remaining_timeout.total_seconds()) await new_timeout.__aenter__() return self._remaining_timeout diff --git a/uv.lock b/uv.lock index a02a8fe411..435b4cf983 100644 --- a/uv.lock +++ b/uv.lock @@ -980,7 +980,7 @@ name = "crawlee" version = "1.10.2" source = { editable = "." } dependencies = [ - { name = "async-timeout" }, + { name = "async-timeout", marker = "python_full_version < '3.11'" }, { name = "cachetools" }, { name = "colorama" }, { name = "impit" }, @@ -1138,7 +1138,7 @@ requires-dist = [ { name = "apify-fingerprint-datapoints", marker = "extra == 'httpx'", specifier = ">=0.0.2" }, { name = "apify-fingerprint-datapoints", marker = "extra == 'playwright'", specifier = ">=0.0.2" }, { name = "apify-fingerprint-datapoints", marker = "extra == 'stagehand'", specifier = ">=0.0.2" }, - { name = "async-timeout", specifier = ">=5.0.1" }, + { name = "async-timeout", marker = "python_full_version < '3.11'", specifier = ">=5.0.1" }, { name = "asyncpg", marker = "extra == 'sql-postgres'", specifier = ">=0.24.0" }, { name = "beautifulsoup4", extras = ["lxml"], marker = "extra == 'beautifulsoup'", specifier = ">=4.12.0" }, { name = "browserforge", marker = "extra == 'adaptive-crawler'", specifier = ">=1.2.4" },