fix(downloader): keep speed-limited batches in a single process

Spawned children re-import the module and start with no TokenBucket, so
--download-processes silently dropped a configured --speed-limit /
global_speed_limit for any 24+ segment batch. Fall back to the in-process
path while a limit is set (same gate as the ranged-parallel paths), log the
fallback, and document the interaction.
This commit is contained in:
imSp4rky
2026-08-07 11:36:17 -06:00
parent cfae8f8882
commit 431aa8fa54
4 changed files with 41 additions and 2 deletions
+16
View File
@@ -1176,6 +1176,8 @@ def requests(
beat the single-interpreter throughput cap (GIL in the ssl read path). Engaged
only when > 1 and there are at least MP_MIN_SEGMENTS urls; otherwise the behaviour
is byte-identical to a single process. Each child runs its own worker pool.
Ignored while a download speed limit is set: spawned children cannot share the
process-global rate budget, so the batch stays in a single process.
index_offset: Added to each url's enumerate index (after ``index_stride`` scaling)
when formatting ``filename`` so a child handling part of a larger batch keeps
its urls' original global indices/names. Internal: set by the multiprocess
@@ -1222,6 +1224,20 @@ def requests(
if not max_workers:
max_workers = min(16, (os.cpu_count() or 1) + 4)
# A spawned child re-imports this module and would start with no TokenBucket, so the
# configured cap would silently stop applying; the cap wins over the fan-out, matching
# the _speed_limiter gates on the ranged-parallel paths.
speed_limiter = _speed_limiter
if processes > 1 and len(urls) >= MP_MIN_SEGMENTS and speed_limiter is not None:
processes = 1
if debug_logger:
debug_logger.log(
level="DEBUG",
operation="downloader_mp_fallback",
message="Speed limit is set; spawned children cannot share its budget, using a single process",
context={"url_count": len(urls), "speed_limit": speed_limiter.rate},
)
# Process fan-out: split a large segment batch across spawned children. Single-URL and small
# batches keep the in-process path (a lone file is already parallelized by ranged parts).
if processes > 1 and len(urls) >= MP_MIN_SEGMENTS: