Executor

class chancy.executors.base.ConcurrentExecutor(worker: Worker, queue: Queue)[source]

Bases: Executor, ABC

Base class for executors based off of the concurrent.futures.Executor class with common functionality.

async cancel(ref: Reference)[source]

Attempt to cancel a running job.

It’s not guaranteed that the job will be cancelled, as it may have already completed by the time this method is called or the executor may not support cancellation.

Parameters:

ref – The reference to the job to cancel.

class chancy.executors.base.Executor(worker: Worker, queue: Queue)[source]

Bases: ABC

The base class for all executors.

Executors are responsible for managing the execution of jobs after they’ve been retrieved from a queue.

See ProcessExecutor and AsyncExecutor for examples of built-in executors.

abstractmethod async cancel(ref: Reference)[source]

Attempt to cancel a running job.

It’s not guaranteed that the job will be cancelled, as it may have already completed by the time this method is called or the executor may not support cancellation.

Parameters:

ref – The reference to the job to cancel.

property concurrency: int[source]
property free_slots: int[source]
abstractmethod get_default_concurrency() int[source]

Get the default concurrency level for this executor.

This method is called when the queue’s concurrency level is set to None. It should return the number of jobs that can be processed concurrently by this executor.

static get_function_and_kwargs(job: QueuedJob) tuple[Callable, dict][source]

Finds the function which should be executed for the given job and returns its keyword arguments.

Parameters:

job – The job instance to get the function and arguments for.

Returns:

A tuple containing the function and its keyword arguments.

async on_job_completed(*, job: QueuedJob, exc: Exception | None = None, result: Any = None)[source]

Called when a job has completed.

This method should be called by the executor when a job has completed execution. It will update the job’s state in the queue and handle retries if necessary.

Parameters:
  • job – The job that has completed.

  • exc – The exception that was raised during execution, if any.

  • result – The result of the job, if any.

async on_job_starting(job: QueuedJob) QueuedJob[source]

Called when a job has been retrieved from the queue and is about to start.

abstractmethod async push(job: QueuedJob)[source]

Push a job onto the job pool.

abstractmethod async stop()[source]

Stop the executor, giving it a chance to clean up any resources it may have allocated to running jobs.

It is not safe to use the executor after this method has been called.