A file system implementation of the request queue client.
This client persists requests to the file system as individual JSON files, making it suitable for scenarios where data needs to survive process restarts. Each request is stored as a separate file in a directory structure following the pattern:
{STORAGE_DIR}/request_queues/{QUEUE_ID}/{REQUEST_ID}.json
The implementation uses RecoverableState to maintain ordering information, in-progress status, and
request handling status. This allows for proper state recovery across process restarts without
embedding metadata in individual request files. File system storage provides durability at the cost of
slower I/O operations compared to memory only-based storage.
This implementation is ideal for long-running crawlers where persistence is important and for situations where you need to resume crawling after process termination.
Hierarchy
- RequestQueueClient
- FileSystemRequestQueueClient
Index
Methods
__init__
- __init__(*, metadata, path_to_rq, lock, recoverable_state): None
Initialize a new instance.
Preferably use the
FileSystemRequestQueueClient.openclass method to create a new instance.Parameters
keyword-onlymetadata: RequestQueueMetadata
keyword-onlypath_to_rq: Path
keyword-onlylock: asyncio.Lock
keyword-onlyrecoverable_state: RecoverableState[RequestQueueState]
Returns None
add_batch_of_requests
- async add_batch_of_requests(requests, *, forefront): AddRequestsResponse
Add batch of requests to the queue.
This method adds a batch of requests to the queue. Each request is processed based on its uniqueness (determined by
unique_key). Duplicates will be identified but not re-added to the queue.Parameters
requests: Sequence[Request]
The collection of requests to add to the queue.
optionalkeyword-onlyforefront: bool = False
Whether to put the added requests at the beginning (True) or the end (False) of the queue. When True, the requests will be processed sooner than previously added requests.
Returns AddRequestsResponse
A response object containing information about which requests were successfully processed and which failed (if any).
drop
- async drop(): None
Drop the whole request queue and remove all its values.
The backend method for the
RequestQueue.dropcall.Returns None
fetch_next_request
- async fetch_next_request(): Request | None
Return the next request in the queue to be processed.
Once you successfully finish processing of the request, you need to call
RequestQueue.mark_request_as_handledto mark the request as handled in the queue. If there was some error in processing the request, callRequestQueue.reclaim_requestinstead, so that the queue will give the request to some other consumer in another call to thefetch_next_requestmethod.Note that the
Nonereturn value does not mean the queue processing finished, it means there are currently no pending requests. To check whether all requests in queue were finished, useRequestQueue.is_finishedinstead.Returns Request | None
The request or
Noneif there are no more pending requests.
get_metadata
- async get_metadata(): RequestQueueMetadata
Get the metadata of the request queue.
Returns RequestQueueMetadata
get_request
- async get_request(unique_key): Request | None
Retrieve a request from the queue.
Parameters
unique_key: str
Unique key of the request to retrieve.
Returns Request | None
The retrieved request, or None, if it did not exist.
is_empty
- async is_empty(): bool
Check if the request queue is empty. That means there are no requests available to fetch.
Returns bool
True if the request queue is empty, False otherwise.
is_finished
- async is_finished(): bool
Check if the request queue is finished.
A finished queue is empty and has no requests currently being processed.
WarningThis default only checks
is_empty, which reports whether requests are available to fetch, not whether requests are still being processed. It can therefore returnTruewhile requests are in progress. Subclasses that track in-progress requests should override this method; all built-in clients already do.Returns bool
True if the request queue is finished, False otherwise.
mark_request_as_handled
- async mark_request_as_handled(request): ProcessedRequest | None
Mark a request as handled after successful processing.
Handled requests will never again be returned by the
RequestQueue.fetch_next_requestmethod.Parameters
request: Request
The request to mark as handled.
Returns ProcessedRequest | None
Information about the queue operation.
Noneif the given request was not in progress.
open
- async open(*, id, name, alias, configuration): Self
Open or create a file system request queue client.
This method attempts to open an existing request queue from the file system. If a queue with the specified ID or name exists, it loads the metadata and state from the stored files. If no existing queue is found, a new one is created.
Parameters
keyword-onlyid: str | None
The ID of the request queue to open. If provided, searches for existing queue by ID.
keyword-onlyname: str | None
The name of the request queue for named (global scope) storages.
keyword-onlyalias: str | None
The alias of the request queue for unnamed (run scope) storages.
keyword-onlyconfiguration: Configuration
The configuration object containing storage directory settings.
Returns Self
An instance for the opened or created storage client.
purge
- async purge(): None
Purge all items from the request queue.
The backend method for the
RequestQueue.purgecall.Returns None
reclaim_request
- async reclaim_request(request, *, forefront): ProcessedRequest | None
Reclaim a failed request back to the queue.
The request will be returned for processing later again by another call to
RequestQueue.fetch_next_request.Parameters
request: Request
The request to return to the queue.
optionalkeyword-onlyforefront: bool = False
Whether to add the request to the head or the end of the queue.
Returns ProcessedRequest | None
Information about the queue operation.
Noneif the given request was not in progress.
Properties
path_to_metadata
The full path to the request queue metadata file.
path_to_rq
The full path to the request queue directory.