sammo.throttler

sammo.throttler#

Provides a context manager for throttling async jobs. The throttling is defined by a list of AtMost instance and can be used to limit the number of concurrent jobs, the number of jobs per time period, the total cost of jobs per time period, or the number of failed jobs per time period. The context manager will block until there is capacity to run the job. The jobs are run in order of priority, breaking ties with creation time.

Module Contents#

Classes#

Throttler

Class that provides flexible throttling for async jobs.

AtMost

Class for defining a throttling limit.

API#

class sammo.throttler.Throttler(limits: list[sammo.throttler.AtMost], sleep_interval: float = 0.01, impute_pending_costs: bool = True, n_cost_samples: int = 10, rejection_window: int | float = 0.5)#

Class that provides flexible throttling for async jobs.

Parameters:
  • limits – A list of sammo.throttler.AtMost instances that define the throttling limits.

  • sleep_interval – The time (in s) between checks for capacity.

  • impute_pending_costs – Whether to estimate the cost of pending jobs with the running average.

  • n_cost_samples – The number of samples to use when calculating the running average.

  • rejection_window – The time (in s) within which a job is considered rejected instead of failed.

Initialization

DEBUG_INTERVAL_SECONDS = 3#
async static sleep(delay: float)#

A more precise sleep function on Windows

update_job_stats(job: sammo.throttler.Job, cost: float | int = 0, failed: bool = False) None#

Update the stats for a job. Needs to be called when a job is finished.

Parameters:
  • job – Job instance to update.

  • cost – The cost of the job.

  • failed – Whether the job failed, default is False.

async wait_in_line(priority: int = 0) sammo.throttler.Job#

Wait async until there is capacity to run a job. The jobs are run in order of priority, breaking ties with creation time.

Parameters:

priority – The priority of the job. Lower numbers are higher priority.

class sammo.throttler.AtMost#

Class for defining a throttling limit.

value: float | int = None#
type: beartype.typing.Literal[calls, running, failed, rejected] = None#
period: float | int = 1#
pause_for: float | int = 0#