Executor¶
- class chancy.executors.base.ConcurrentExecutor(worker, queue)[source]¶
-
Base class for executors based off of the concurrent.futures.Executor class with common functionality.
- async cancel(ref)[source]¶
Attempt to cancel every running execution of the referenced 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 (Reference) – The reference to the job to cancel.
- async cancel_execution(job)[source]¶
Attempt to cancel only the execution identified by
job.claim_id.Recovery uses this optional hook instead of job-wide cancellation. Custom executors must match both the job ID and claim ID. The default does nothing; database fencing still rejects the abandoned execution’s updates when cancellation is unsupported or unsuccessful.
- Parameters:
job (QueuedJob)
- class chancy.executors.base.Executor(worker, queue)[source]¶
Bases:
ABCThe base class for all executors.
Executors are responsible for managing the execution of jobs after they’ve been retrieved from a queue.
See
ProcessExecutorandAsyncExecutorfor examples of built-in executors.- abstractmethod async cancel(ref)[source]¶
Attempt to cancel every running execution of the referenced 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 (Reference) – The reference to the job to cancel.
- async cancel_execution(job)[source]¶
Attempt to cancel only the execution identified by
job.claim_id.Recovery uses this optional hook instead of job-wide cancellation. Custom executors must match both the job ID and claim ID. The default does nothing; database fencing still rejects the abandoned execution’s updates when cancellation is unsupported or unsuccessful.
- Parameters:
job (QueuedJob)
- classmethod get_capabilities()[source]¶
Return the features supported by this executor.
- Return type:
- abstractmethod get_default_concurrency()[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.
- Return type:
- classmethod get_function_and_kwargs(job)[source]¶
Find the function which should be executed for the given job and return its keyword arguments.
Subclasses can override this method to resolve the function differently or to inject additional keyword arguments, such as application-level dependencies, before the job runs. Every built-in executor goes through this method via
prepare_job_for_execution().Note
This method runs wherever the executor runs the job: in the worker’s event loop, in a pool thread, in a sub-interpreter or in a child process. For process and sub-interpreter executors, the executor subclass must be importable by name, and injected resources should be created in that execution context (for example from
on_initialize_worker). Async and threaded executors share the worker’s memory; injected resources must be compatible with the event loop or thread where the job runs.
- async on_job_completed(*, job, exc=None, result=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 (QueuedJob) – The job that has completed.
exc (BaseException | None) – The exception that was raised during execution, if any.
result (Any) – The result of the job, if any.
- async on_job_starting(job)[source]¶
Called when a job has been retrieved from the queue and is about to start.
- classmethod prepare_job_for_execution(job)[source]¶
Validate and prepare a job according to executor capabilities.
- static run_function(func, kwargs)[source]¶
Run a synchronous or asynchronous function to completion.
- 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.
- classmethod supports(capability)[source]¶
Return whether this executor supports all given capabilities.
- Parameters:
capability (Capability)
- Return type: