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
ProcessPoolExecutorofmax_workersprocesses (default_worker_count()whenNone), always with thespawnstart method:fork(Linux’s default) would copy a live event loop andaiosqlitethreads, which don’t survive it.
- async synology_apm_repo.sdk.concurrency.shutdown_pool(executor)¶
Shuts
executordown — 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
Nonewhendescriptorsis empty, holds aNone, 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, orWorkerProcessErrorwhen a worker process died.- Raises:
WorkerProcessError – A worker process died.
- synology_apm_repo.sdk.concurrency.run_in_worker_loop(coro)¶
Runs
coroon this worker process’s persistent event loop, so worker-lifetime resources bound to it survive between calls (unlikeasyncio.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 anatexithook 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.stderrwith a stream whosefileno()returns a sentinel (Textual’s output capture does): launched later, the tracker passes that fd on and the first process pool fails withValueError: 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 mostmax_concurrentat once, for I/O-bound work on the event loop (dispatch_to_poolis for CPU-bound work).on_done(item)runs after its slot is freed.- Raises:
ExceptionGroup – One or more
worker/on_donecalls 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 overitemswould: 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)onexecutorfor every item, at mostmax_concurrentat 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, notitemsorder.- Raises:
ExceptionGroup – One or more items failed (see
raise_worker_failure); the rest were cancelled.