Other Classes

The following classes are provided for complete documentation. You should not normally need to work with them directly.

Rate Limit

class prawcore.rate_limit.RateLimiter(*, window_size)

Facilitates the rate limiting of requests to Reddit.

Rate limits are controlled based on feedback from requests to Reddit.

Parameters:

window_size (int)

call(request_function, set_header_callback, *args, **kwargs)

Rate limit the call to request_function.

Parameters:
  • request_function (Callable[[Any], Response]) – A function call that returns an HTTP response object.

  • set_header_callback (Callable[[], dict[str, str]]) – A callback function used to set the request headers. This callback is called after any necessary sleep time occurs.

  • args (Any) – The positional arguments to request_function.

  • kwargs (Any) – The keyword arguments to request_function.

Return type:

Response

delay()

Sleep for an amount of time to remain under the rate limit.

Return type:

None

update(response_headers)

Update the state of the rate limiter based on the response headers.

This method should only be called following an HTTP request to Reddit.

Response headers that do not contain x-ratelimit fields will be treated as a single request. This behavior is to error on the safe-side as such responses should trigger exceptions that indicate invalid behavior.

Return type:

None

Parameters:

response_headers (Mapping[str, str])

Retry Strategies

class prawcore.sessions.RetryStrategy

An abstract class for scheduling request retries.

The strategy controls both the number and frequency of retry attempts.

Instances of this class are immutable.

abstractmethod consume_available_retry()

Allow one fewer retry.

Return type:

RetryStrategy

abstractmethod should_retry_on_failure()

Return True when a retry should occur.

Return type:

bool

sleep()

Sleep until we are ready to attempt the request.

Return type:

None

class prawcore.sessions.FiniteRetryStrategy(retries=2)

A RetryStrategy that retries requests a finite number of times.

Parameters:

retries (int)

consume_available_retry()

Allow one fewer retry.

Return type:

FiniteRetryStrategy

should_retry_on_failure()

Return True if and only if the strategy will allow another retry.

Return type:

bool

sleep()

Sleep until we are ready to attempt the request.

Return type:

None