Configuration¶
LeasedExecutorManager¶
manager = LeasedExecutorManager(
backend="thread",
max_pools=4,
min_pools=1,
units_per_pool=10,
size_provider=lambda: len(connected_devices),
check_interval=120.0,
default_lease_seconds=300.0,
lease_grace_seconds=15.0,
workers_per_pool=4,
name_prefix="leasepool",
)
backend"thread","process", or"interpreter". You may also pass the matchingExecutorBackendenum value.max_poolsMaximum number of executor objects owned by this manager. Required.
min_poolsMinimum desired number of executor objects. The manager creates this many on startup unless adaptive sizing asks for more.
units_per_poolAdaptive sizing ratio used with
size_provider.size_providerOptional callable returning a current unit count. Units can be connected devices, tenants, queues, shards, customers, or any custom runtime signal.
check_intervalHow often the background checker wakes when no scale or expiry event occurs. Expiring leases and
notify_scale_changed()can wake it earlier.default_lease_secondsDefault lease duration when callers do not specify
lease_secondsinacquire().lease_grace_secondsExtra time after soft expiry before hard revocation.
workers_per_poolWorker count passed to the executor constructor as
max_workers.name_prefixPrefix for worker thread names on backends that support
thread_name_prefix.loggerOptional logger used by the manager for lifecycle, lease, and checker logs.
process_loggingOptional
ProcessLoggingConfigfor process-worker log forwarding.forward_process_logsConvenience flag for enabling process-worker log forwarding without creating a
ProcessLoggingConfigmanually.process_log_levelChild-process root logger level when process log forwarding is enabled.
process_log_target_loggerParent-process logger that receives forwarded worker log records.
clear_process_log_handlersWhether to remove existing child-process root handlers before installing the queue handler. Defaults to
Trueto avoid duplicate logs after fork.**executor_kwargsExtra keyword arguments passed to the underlying executor constructor. For the process backend this can include options such as
initializer,initargs,mp_context, ormax_tasks_per_child.
Validation notes¶
max_pools, min_pools, units_per_pool, and workers_per_pool must
be positive integer-like values. Fractional values are rejected instead of being
silently truncated.
check_interval and default_lease_seconds must be finite numbers greater
than zero.
lease_grace_seconds must be a finite number greater than or equal to zero.
Use lease_grace_seconds=0.0 for no grace period.
Per-call lease_seconds passed to manager.acquire() must be a finite
number greater than zero.
Process logging configuration¶
Use the explicit configuration object when you want documented, reusable configuration:
import logging
from leasepool import LeasedExecutorManager, ProcessLoggingConfig
manager = LeasedExecutorManager(
backend="process",
max_pools=1,
min_pools=1,
workers_per_pool=2,
process_logging=ProcessLoggingConfig(
enabled=True,
level="INFO",
target_logger=logging.getLogger("myapp.process-workers"),
),
)
Use the convenience arguments for a shorter setup:
manager = LeasedExecutorManager(
backend="process",
max_pools=1,
min_pools=1,
workers_per_pool=2,
forward_process_logs=True,
process_log_level="INFO",
)
Do not pass process_logging and the forward_process_logs /
process_log_* convenience arguments together.
Acquire options¶
lease = await manager.acquire(
lease_seconds=30.0,
owner="request:123",
wait=True,
timeout=2.0,
)
lease_secondsOverrides
default_lease_secondsfor this lease.ownerOptional diagnostic label.
waitIf
True, wait for capacity when every pool is leased. IfFalse, raiseLeaseUnavailableErrorimmediately.timeoutMaximum time to wait for capacity. Raises built-in
TimeoutErrorif the timeout is reached.
WorkGrinder¶
grinder = WorkGrinder(
executor_manager=manager,
max_wait_seconds=10.0,
batch_size_threshold=20,
lease_seconds=60.0,
owner_prefix="work-grinder",
)
executor_managerStarted
LeasedExecutorManagerused for leases.max_wait_secondsMaximum age of the oldest pending item before a batch is processed.
batch_size_thresholdPending item count that triggers immediate batch processing.
lease_secondsLease duration requested for each batch.
owner_prefixPrefix used to label batch leases in manager diagnostics.
Loop ownership¶
WorkGrinder async methods are bound to the event loop that called
await grinder.start().
Call these from the owning event loop:
await grinder.submit(...);await grinder.enqueue(...);await grinder.stop(...);await grinder.astats();grinder.stats()while the grinder is running.
Call these from other OS threads:
grinder.submit_from_thread(...);grinder.stats_from_thread(...).
max_pools is per manager¶
max_pools is not global across all backend types.
thread_manager = LeasedExecutorManager(
backend="thread",
max_pools=5,
workers_per_pool=4,
)
process_manager = LeasedExecutorManager(
backend="process",
max_pools=1,
workers_per_pool=4,
)
This allows up to five thread executors and one process executor. Each manager enforces its own limit.