SubInterpreterExecutor

class chancy.executors.sub.SubInterpreterExecutor(worker: Worker, queue: Queue)[source]

Bases: ConcurrentExecutor

Note

This executor is experimental and may not work as expected. The sub-interpreter features it is built on is experimental and only available in Python 3.13 (with a backport) and >3.14.

To use with 3.13, run:

pip install chancy[sub]

To use this executor, simply pass the import path to this class in the executor field of your queue configuration or use the Executor shortcut:

async with Chancy("postgresql://localhost/postgres") as chancy:
    await chancy.declare(
        Queue(
            name="default",
            executor=Chancy.Executor.SubInterpreter,
        )
    )

It’s important to note that many C-based Python libraries are not (yet) compatible with sub-interpreters. If you encounter issues, you may need to switch to a different executor.

Parameters:
  • worker – The worker instance associated with this executor.

  • queue – The queue that this executor is associated with.

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.

On Python 3.13+, defaults to the number of logical CPUs on the system plus 4. On older versions of Python, defaults to the number of CPUs on the system plus 4. This mimics the behavior of Python’s built-in ThreadPoolExecutor.

classmethod job_wrapper(job: QueuedJob) tuple[QueuedJob, Any][source]

This is the function that is actually started by the sub-interpreter executor. It’s responsible for setting up necessary limits, running the job, and returning the result.

static on_initialize_worker(parent_sys_path: list[str])[source]

This method is called in each worker before it begins running jobs. It can be used to perform any necessary setup, such as loading NLTK datasets or calling django.setup().

By default, it replaces the running job’s sys.path with the workers.

async push(job: QueuedJob) Future[source]

Push a job onto the job pool.

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.