Skip to main content
Version: Next

AsyncThread

Run an asyncio event loop in a dedicated background thread.

This lets synchronous Scrapy callbacks drive asynchronous Apify and Crawlee coroutines. The scheduler and the HTTP cache storage each own their own AsyncThread, so the request queue and the key-value store never share an event loop; they only share the read-only global Configuration. A single shared loop would also work but would couple their lifecycles.

Index

Methods

__init__

  • __init__(default_timeout): None
  • Parameters

    • optionaldefault_timeout: timedelta = timedelta(seconds=60)

    Returns None

close

  • close(timeout): None
  • Close the event loop and its thread gracefully.

    This method cancels all pending tasks, stops the event loop, and waits for the thread to exit. If the thread does not exit within the given timeout, a forced shutdown is attempted.


    Parameters

    • optionaltimeout: timedelta | None = None

      The maximum time to wait for the event loop thread to exit. Pass None to use the default_timeout passed to the constructor.

    Returns None

run_coro

  • run_coro(coro, timeout): Any
  • Run a coroutine on an event loop running in a separate thread.

    This method schedules the coroutine to run on the event loop and blocks until the coroutine completes or the specified timeout is reached.


    Parameters

    • coro: Coroutine

      The coroutine to run.

    • optionaltimeout: timedelta | Literal[default] = 'default'

      The maximum time to wait for the coroutine to finish. Pass 'default' to use the default_timeout passed to the constructor.

    Returns Any

    The result returned by the coroutine.

Page Options