synology_apm_repo.sdk.concurrency module

Multi-core parallelism for CPU-bound work (decompress/decrypt/hash) through a ProcessPoolExecutor (asyncio.to_thread() keeps decrypt and the decode loop on one core under the GIL): pool sizing and construction, load-balanced dispatch, worker-failure reporting and the worker-side event loop, plus bounded_gather/ bounded_gather_first_error for I/O-bound fan-out.

synology_apm_repo.sdk.concurrency.default_worker_count()

clamp(os.cpu_count() // 2, 1, 8), the size of every worker pool. Halved because on a hybrid (performance + efficiency) chip a batch waits for its slowest task, so efficiency cores add little.

synology_apm_repo.sdk.concurrency.new_process_pool(*, max_workers=None, initializer=None, initargs=())

A ProcessPoolExecutor of max_workers processes (default_worker_count() when None), always with the spawn start method: fork (Linux’s default) would copy a live event loop and aiosqlite threads, which don’t survive it.

async synology_apm_repo.sdk.concurrency.shutdown_pool(executor)

Shuts executor down — queued items cancelled, running ones waited for — off the event loop, so the wait doesn’t freeze it.

synology_apm_repo.sdk.concurrency.common_descriptor(descriptors)

The one descriptor every entry equals, or None when descriptors is empty, holds a None, or disagrees: whether several units of work can share one worker pool, whose processes are bound to one descriptor for their lifetime.

synology_apm_repo.sdk.concurrency.first_failure(group, *, operation)

The failure an exception group of concurrent tasks (worker dispatches, or in-process reads) stands for: its first exception that is not a cancellation, with a note counting the others. A group of only cancellations is returned unchanged.

synology_apm_repo.sdk.concurrency.raise_worker_failure(group, *, operation)

Raises what a worker dispatch’s exception group stands for: first_failure, or WorkerProcessError when a worker process died.

Raises:

WorkerProcessError – A worker process died.

synology_apm_repo.sdk.concurrency.run_in_worker_loop(coro)

Runs coro on this worker process’s persistent event loop, so worker-lifetime resources bound to it survive between calls (unlike asyncio.run()’s loop per call). close_worker_loop() releases it at worker shutdown.

synology_apm_repo.sdk.concurrency.close_worker_loop()

Closes this worker process’s persistent event loop, after its resources were released through a last run_in_worker_loop(); for an atexit hook the pool initializer registers. A no-op if no loop was created.

synology_apm_repo.sdk.concurrency.preload_resource_tracker()

Launches multiprocessing’s resource-tracker process now. Call it before replacing sys.stderr with a stream whose fileno() returns a sentinel (Textual’s output capture does): launched later, the tracker passes that fd on and the first process pool fails with ValueError: bad value(s) in fds_to_keep. A no-op off POSIX.

async synology_apm_repo.sdk.concurrency.bounded_gather(items, worker, *, max_concurrent, on_done=None)

Runs worker(item) for every item, at most max_concurrent at once, for I/O-bound work on the event loop (dispatch_to_pool is for CPU-bound work). on_done(item) runs after its slot is freed.

Raises:

ExceptionGroup – One or more worker/on_done calls failed; the rest were cancelled.

async synology_apm_repo.sdk.concurrency.bounded_gather_first_error(items, worker, *, max_concurrent)

bounded_gather, but failing as a serial loop over items would: with the error of the earliest item that failed, raised bare. A failure cancels no running item; an item after it that has not started yet is skipped.

Raises:

Exception – The earliest failed item’s error.

async synology_apm_repo.sdk.concurrency.dispatch_to_pool(executor, worker_fn, items, *, max_concurrent, on_result=None)

Runs worker_fn(item) on executor for every item, at most max_concurrent at once, each free worker taking the next item, so uneven per-item cost balances itself. on_result(item, result) is awaited in this process as each one finishes. Results come back in completion order, not items order.

Raises:

ExceptionGroup – One or more items failed (see raise_worker_failure); the rest were cancelled.