@simplexai/localmodels-linux-arm64 0.1.0-dev.60 → 0.1.0-dev.62
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bootstrap.identity +1 -1
- package/bootstrap.sha256 +78 -64
- package/modules/localmodels/__init__.py +1 -1
- package/modules/localmodels/__main__.py +5 -3
- package/modules/localmodels/auth_proxy.py +185 -19
- package/modules/localmodels/catalog.json +363 -0
- package/modules/localmodels/catalog.py +1 -1
- package/modules/localmodels/catalog_add.py +5 -5
- package/modules/localmodels/catalog_license.py +2 -0
- package/modules/localmodels/catalog_recipes.py +2 -0
- package/modules/localmodels/cli_parser.py +6 -2
- package/modules/localmodels/context_policy.py +40 -4
- package/modules/localmodels/custom_hub_http.py +27 -104
- package/modules/localmodels/debug_mode.py +13 -0
- package/modules/localmodels/debug_stub_server.py +156 -1
- package/modules/localmodels/deploy.py +11 -2
- package/modules/localmodels/download-mirrors.json +12 -0
- package/modules/localmodels/engines-b12757-mix-ef2df21.json +26 -0
- package/modules/localmodels/engines-whisper-b5454.json +120 -1
- package/modules/localmodels/gateway_app_bifrost.py +14 -14
- package/modules/localmodels/gateway_bifrost.py +4 -1
- package/modules/localmodels/gateway_cli.py +14 -80
- package/modules/localmodels/gateway_server.py +88 -16
- package/modules/localmodels/gpu_specs.py +35 -1
- package/modules/localmodels/harness.py +1 -2
- package/modules/localmodels/hf_repo.py +12 -4
- package/modules/localmodels/install.py +4 -4
- package/modules/localmodels/library_jobs.py +4 -0
- package/modules/localmodels/library_serve.py +69 -13
- package/modules/localmodels/memcalc_load.py +9 -4
- package/modules/localmodels/npm_transport.py +4 -58
- package/modules/localmodels/recommend_cli.py +2 -1
- package/modules/localmodels/remote_ops.py +120 -713
- package/modules/localmodels/remote_ops_common.py +670 -0
- package/modules/localmodels/remote_ops_download.py +399 -381
- package/modules/localmodels/remote_ops_mirrors.py +27 -66
- package/modules/localmodels/remote_ops_prepare.py +43 -35
- package/modules/localmodels/remote_ops_ranges.py +102 -63
- package/modules/localmodels/remote_ops_redirect.py +393 -0
- package/modules/localmodels/remote_ops_remove.py +30 -22
- package/modules/localmodels/remote_ops_service.py +102 -95
- package/modules/localmodels/remote_ops_verify.py +94 -63
- package/modules/localmodels/runtime_cli.py +15 -5
- package/modules/localmodels/runtime_io.py +54 -1
- package/modules/localmodels/source_cli.py +136 -23
- package/modules/localmodels/speech-sv.txt +93 -0
- package/modules/localmodels/speech_admission.py +437 -0
- package/modules/localmodels/speech_catalog.py +170 -0
- package/modules/localmodels/speech_cli.py +273 -0
- package/modules/localmodels/speech_jobs.py +776 -0
- package/modules/localmodels/speech_model.py +404 -0
- package/modules/localmodels/speech_serve.py +287 -0
- package/modules/localmodels/speech_server.py +837 -0
- package/modules/localmodels/speech_state.py +697 -0
- package/modules/localmodels/speech_worker_sensevoice.py +255 -0
- package/modules/localmodels/speech_workers.py +783 -0
- package/modules/localmodels/stack.py +0 -33
- package/modules/localmodels/stack_auto.py +5 -3
- package/modules/localmodels/stack_aux_gpu.py +45 -4
- package/modules/localmodels/stack_capacity.py +77 -8
- package/modules/localmodels/stack_cli.py +16 -1
- package/modules/localmodels/stack_compose.py +1 -1
- package/modules/localmodels/stack_down.py +24 -1
- package/modules/localmodels/stack_hardware.py +57 -88
- package/modules/localmodels/stack_hardware_text.py +4 -59
- package/modules/localmodels/stack_memory.py +13 -3
- package/modules/localmodels/stack_suite.py +9 -10
- package/modules/localmodels/stack_summary.py +4 -4
- package/modules/localmodels/stack_view_llm.py +1 -1
- package/modules/localmodels/transport.py +4 -2
- package/modules/localmodels/tui_views.py +2 -2
- package/modules/{simplex_localmodels-0.1.0.dev60.dist-info → simplex_localmodels-0.1.0.dev62.dist-info}/METADATA +1 -1
- package/modules/{simplex_localmodels-0.1.0.dev60.dist-info → simplex_localmodels-0.1.0.dev62.dist-info}/RECORD +75 -61
- package/modules/{simplex_localmodels-0.1.0.dev60.dist-info → simplex_localmodels-0.1.0.dev62.dist-info}/licenses/NOTICE +9 -0
- package/package.json +1 -1
- package/tool.json +1 -1
- /package/modules/{simplex_localmodels-0.1.0.dev60.dist-info → simplex_localmodels-0.1.0.dev62.dist-info}/WHEEL +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev60.dist-info → simplex_localmodels-0.1.0.dev62.dist-info}/entry_points.txt +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev60.dist-info → simplex_localmodels-0.1.0.dev62.dist-info}/licenses/localmodels/HERMES-LICENSE +0 -0
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
2
|
"""Parallel byte-range transfer of one file, with connections that rebalance ranges.
|
|
3
3
|
|
|
4
|
-
Part of the remote_ops bundle (transport.BUNDLES): standard library only, Python 3.8+.
|
|
5
|
-
tunables
|
|
6
|
-
|
|
4
|
+
Part of the remote_ops bundle (transport.BUNDLES): standard library only, Python 3.8+. Owns the
|
|
5
|
+
range-transfer tunables and SlowTransferError so ``remote_ops_download`` imports strictly
|
|
6
|
+
downward without any cycle.
|
|
7
7
|
"""
|
|
8
8
|
|
|
9
9
|
from __future__ import annotations
|
|
@@ -17,61 +17,102 @@ import statistics
|
|
|
17
17
|
import threading
|
|
18
18
|
import time
|
|
19
19
|
import urllib.error
|
|
20
|
-
import urllib.request
|
|
21
20
|
from typing import Any
|
|
22
21
|
|
|
23
22
|
try:
|
|
24
|
-
from localmodels import
|
|
25
|
-
from localmodels import remote_ops_download as download
|
|
23
|
+
from localmodels import remote_ops_common, remote_ops_mirrors
|
|
26
24
|
except ImportError: # run as a bare file from this directory
|
|
27
|
-
import
|
|
28
|
-
import remote_ops_download as download # type: ignore[import-not-found,no-redef]
|
|
25
|
+
import remote_ops_common # type: ignore[import-not-found,no-redef]
|
|
29
26
|
import remote_ops_mirrors # type: ignore[import-not-found,no-redef]
|
|
30
27
|
|
|
28
|
+
# Switch away from a transfer whose sustained throughput over SLOW_WINDOW_S stays below
|
|
29
|
+
# SLOW_BYTES_PER_S while more than SLOW_MIN_REMAINING_S of transfer time remains.
|
|
30
|
+
SLOW_WINDOW_S = 15.0
|
|
31
|
+
SLOW_BYTES_PER_S = 256 * 1024
|
|
32
|
+
SLOW_MIN_REMAINING_S = 30.0
|
|
33
|
+
# A file is fetched over up to CONNECTIONS parallel byte ranges of at least MIN_SEGMENT_BYTES
|
|
34
|
+
# each. A connection that finishes its range takes over the second part of the range that will
|
|
35
|
+
# finish last (work stealing), split by the two connections' rates so both end together; a
|
|
36
|
+
# stalled connection loses all of its range. A piece is stolen only when the thief would spend
|
|
37
|
+
# at least STEAL_MIN_S on it, so the extra request (TLS, a Hub redirect) is worth it. Counts come
|
|
38
|
+
# from the 2026-11-24 measurements in docs/local-models/network-download-optimization.md: from
|
|
39
|
+
# China the Hugging Face CDN (also behind hf-mirror) caps each connection near 0.1 MB/s, so 16
|
|
40
|
+
# connections fetch about 16 times as fast; ModelScope reaches the link's ~100 MB/s with 8.
|
|
41
|
+
CONNECTIONS = 8
|
|
42
|
+
SOURCE_CONNECTIONS: dict[str, int] = {"huggingface": 16, "hf-mirror": 16}
|
|
43
|
+
MIN_SEGMENT_BYTES = 64 << 20
|
|
44
|
+
STEAL_MIN_S = 6.0
|
|
45
|
+
STEAL_MIN_BYTES = 8 << 20 # while no connection's rate is known yet, halve ranges this large
|
|
46
|
+
CLAIM_GRACE_S = 3.0 # a connection's rate is unknown until this long after its first byte
|
|
47
|
+
# A connection with no first byte after this long counts as stalled. The first byte of a Hub
|
|
48
|
+
# range took up to 3 s (a redirect, then TLS to the CDN), so a slow start is not a stall.
|
|
49
|
+
CLAIM_STALL_S = 20.0
|
|
50
|
+
CLAIM_RATE_WINDOW_S = 10.0
|
|
51
|
+
STEAL_POLL_S = 0.5 # how often an idle connection looks for a range to steal
|
|
52
|
+
ATTEMPTS = 5
|
|
53
|
+
# A connection's progress moves once per read, so reads stay small: an 8 MiB read at 2-3 MB/s
|
|
54
|
+
# per connection made the 2 s rate jump in 4 MiB/s steps (often 0). The rate shown is the
|
|
55
|
+
# average over RATE_WINDOW_S, so a stall or burst of one connection does not swing the ETA.
|
|
56
|
+
READ_BYTES = 1 << 20
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class SlowTransferError(RuntimeError):
|
|
60
|
+
"""Sustained transfer throughput dropped below SLOW_BYTES_PER_S; switch to another host."""
|
|
61
|
+
|
|
62
|
+
def __init__(self, bytes_per_s: int) -> None:
|
|
63
|
+
self.bytes_per_s = bytes_per_s
|
|
64
|
+
super().__init__(
|
|
65
|
+
f"download too slow ({bytes_per_s / 1024:.0f} KB/s < "
|
|
66
|
+
f"{SLOW_BYTES_PER_S / 1024:.0f} KB/s)"
|
|
67
|
+
)
|
|
68
|
+
|
|
31
69
|
|
|
32
|
-
class
|
|
70
|
+
class NoRangeError(Exception):
|
|
33
71
|
"""The source stopped honouring byte ranges; the next source is tried."""
|
|
34
72
|
|
|
35
73
|
|
|
36
|
-
def connections_for(source:
|
|
74
|
+
def connections_for(source: remote_ops_common.Source) -> int:
|
|
37
75
|
"""How many parallel ranges to open on ``source`` (a few hosts throttle each connection)."""
|
|
38
76
|
return max(
|
|
39
77
|
1,
|
|
40
|
-
|
|
78
|
+
SOURCE_CONNECTIONS.get(source.get("source", ""), CONNECTIONS),
|
|
41
79
|
)
|
|
42
80
|
|
|
43
81
|
|
|
44
82
|
def _split(start: int, end: int, count: int | None = None) -> list[list[int]]:
|
|
45
83
|
"""[start, end, done] segments of [start, end): ``count`` (default CONNECTIONS) of them when
|
|
46
84
|
each gets at least MIN_SEGMENT_BYTES, fewer otherwise, never none for a non-empty range."""
|
|
47
|
-
wanted =
|
|
48
|
-
count = max(1, min(wanted, (end - start) //
|
|
85
|
+
wanted = CONNECTIONS if count is None else count
|
|
86
|
+
count = max(1, min(wanted, (end - start) // MIN_SEGMENT_BYTES))
|
|
49
87
|
step = -(-(end - start) // count)
|
|
50
88
|
return [[s, min(end, s + step), s] for s in range(start, end, step)]
|
|
51
89
|
|
|
52
90
|
|
|
53
|
-
def
|
|
91
|
+
def resume_ranges(part: str, size: int, count: int | None = None) -> list[list[int]]:
|
|
54
92
|
"""The segments still to fetch. A plain .part (a single-connection download of an earlier
|
|
55
93
|
version) is a finished prefix; the file is then sized so each range writes in place."""
|
|
56
|
-
segments =
|
|
94
|
+
segments = remote_ops_common.load_ranges(part, size)
|
|
57
95
|
if segments is None:
|
|
58
96
|
have = os.path.getsize(part) if os.path.exists(part) else 0
|
|
59
97
|
with open(part, "ab") as out:
|
|
60
98
|
out.truncate(size)
|
|
61
99
|
segments = _split(have, size, count) if have < size else []
|
|
62
|
-
|
|
100
|
+
remote_ops_common.save_ranges(part, size, segments)
|
|
63
101
|
return segments
|
|
64
102
|
|
|
65
103
|
|
|
66
104
|
def _retryable(exc: BaseException) -> bool:
|
|
67
105
|
"""Permanent HTTP responses (unfollowed redirects such as 308, client errors other than
|
|
68
|
-
408/429) will not turn into the pinned file on retry; move on
|
|
106
|
+
408/429) and redirect policy refusals will not turn into the pinned file on retry; move on
|
|
107
|
+
to the next source at once."""
|
|
108
|
+
if isinstance(exc, remote_ops_common.UnsafeRedirectError):
|
|
109
|
+
return False
|
|
69
110
|
if isinstance(exc, urllib.error.HTTPError):
|
|
70
111
|
return exc.code in (408, 429) or exc.code >= 500
|
|
71
112
|
return True
|
|
72
113
|
|
|
73
114
|
|
|
74
|
-
class
|
|
115
|
+
class Claim:
|
|
75
116
|
"""One connection's hold on a segment, with recent (time, done) marks for its rate."""
|
|
76
117
|
|
|
77
118
|
def __init__(self, segment: list[int], now: float) -> None:
|
|
@@ -84,7 +125,7 @@ class _Claim:
|
|
|
84
125
|
if self.first is None:
|
|
85
126
|
self.first = now
|
|
86
127
|
self.marks.append((now, self.segment[2]))
|
|
87
|
-
while len(self.marks) > 2 and now - self.marks[1][0] >=
|
|
128
|
+
while len(self.marks) > 2 and now - self.marks[1][0] >= CLAIM_RATE_WINDOW_S:
|
|
88
129
|
self.marks.popleft()
|
|
89
130
|
|
|
90
131
|
def rate(self, now: float) -> float | None:
|
|
@@ -92,14 +133,14 @@ class _Claim:
|
|
|
92
133
|
byte yet, or less than CLAIM_GRACE_S since it), 0 with no first byte after
|
|
93
134
|
CLAIM_STALL_S. A stalled connection adds no marks, so its rate falls towards zero."""
|
|
94
135
|
if self.first is None:
|
|
95
|
-
return 0.0 if now - self.since >=
|
|
96
|
-
if now - self.first <
|
|
136
|
+
return 0.0 if now - self.since >= CLAIM_STALL_S else None
|
|
137
|
+
if now - self.first < CLAIM_GRACE_S:
|
|
97
138
|
return None
|
|
98
139
|
then, before = self.marks[0]
|
|
99
140
|
return (self.segment[2] - before) / max(1e-3, now - then)
|
|
100
141
|
|
|
101
142
|
|
|
102
|
-
class
|
|
143
|
+
class Transfer:
|
|
103
144
|
"""Parallel ranged download of one file. Each connection claims a segment and fetches it
|
|
104
145
|
from its ``done`` offset with retries, then claims another or steals the tail of the one
|
|
105
146
|
that will finish last. ``done`` advances only after the bytes are written and never past
|
|
@@ -117,12 +158,12 @@ class _Transfer:
|
|
|
117
158
|
connections: int = 1,
|
|
118
159
|
) -> None:
|
|
119
160
|
self.url, self.part, self.size, self.segments = url, part, size, segments
|
|
120
|
-
self.attempts =
|
|
161
|
+
self.attempts = ATTEMPTS if attempts is None else max(1, attempts)
|
|
121
162
|
self.connections = max(1, connections)
|
|
122
163
|
self.lock, self.io_lock = threading.Lock(), threading.Lock()
|
|
123
164
|
self.stop = threading.Event()
|
|
124
165
|
self.errors: list[BaseException] = []
|
|
125
|
-
self.claims: dict[int,
|
|
166
|
+
self.claims: dict[int, Claim] = {} # id(segment) -> its connection's claim
|
|
126
167
|
self.steals = 0
|
|
127
168
|
self._responses: list[Any] = []
|
|
128
169
|
|
|
@@ -165,7 +206,7 @@ class _Transfer:
|
|
|
165
206
|
if claim is None:
|
|
166
207
|
if not self.active():
|
|
167
208
|
return
|
|
168
|
-
self.stop.wait(
|
|
209
|
+
self.stop.wait(STEAL_POLL_S) # stay to take over a range that stalls later
|
|
169
210
|
continue
|
|
170
211
|
try:
|
|
171
212
|
self.worker(claim.segment, claim)
|
|
@@ -174,7 +215,7 @@ class _Transfer:
|
|
|
174
215
|
pace = claim.rate(time.monotonic())
|
|
175
216
|
self.claims.pop(id(claim.segment), None)
|
|
176
217
|
|
|
177
|
-
def _claim(self, pace: float | None) ->
|
|
218
|
+
def _claim(self, pace: float | None) -> Claim | None:
|
|
178
219
|
now, victim = time.monotonic(), None
|
|
179
220
|
with self.lock:
|
|
180
221
|
if self.stop.is_set():
|
|
@@ -186,19 +227,19 @@ class _Transfer:
|
|
|
186
227
|
segment, victim = self._steal(now, pace)
|
|
187
228
|
if segment is None:
|
|
188
229
|
return None
|
|
189
|
-
claim = self.claims[id(segment)] =
|
|
230
|
+
claim = self.claims[id(segment)] = Claim(segment, now)
|
|
190
231
|
response = victim.response if victim is not None else None
|
|
191
232
|
if response is not None: # a stalled victim gave up all of its range: unblock it
|
|
192
233
|
_close(response)
|
|
193
234
|
return claim
|
|
194
235
|
|
|
195
|
-
def _steal(self, now: float, pace: float | None) -> tuple[list[int] | None,
|
|
236
|
+
def _steal(self, now: float, pace: float | None) -> tuple[list[int] | None, Claim | None]:
|
|
196
237
|
"""Under ``lock``: split off the tail of the claim that will finish last and return it
|
|
197
238
|
(and the victim when it keeps nothing), or (None, None) when no split is worth it."""
|
|
198
239
|
rates = [r for r in (c.rate(now) for c in self.claims.values()) if r]
|
|
199
240
|
peer = statistics.median(rates) if rates else None
|
|
200
241
|
thief = pace if pace else peer # only measured, moving rates
|
|
201
|
-
best: tuple[float,
|
|
242
|
+
best: tuple[float, Claim, int] | None = None
|
|
202
243
|
for claim in self.claims.values():
|
|
203
244
|
left = claim.segment[1] - claim.segment[2]
|
|
204
245
|
share = _share(left, claim.rate(now), thief) if left > 0 else None
|
|
@@ -215,12 +256,14 @@ class _Transfer:
|
|
|
215
256
|
self.steals += 1
|
|
216
257
|
return stolen, (claim if keep == 0 else None)
|
|
217
258
|
|
|
218
|
-
def _range(self, segment: list[int], claim:
|
|
259
|
+
def _range(self, segment: list[int], claim: Claim) -> None:
|
|
219
260
|
with self.lock:
|
|
220
261
|
done, end = segment[2], segment[1]
|
|
221
262
|
if done >= end:
|
|
222
263
|
return
|
|
223
|
-
with
|
|
264
|
+
with remote_ops_common.open_url(
|
|
265
|
+
remote_ops_common.build_request(self.url, done, end), timeout=60
|
|
266
|
+
) as r:
|
|
224
267
|
self._track_response(r)
|
|
225
268
|
claim.response = r
|
|
226
269
|
try:
|
|
@@ -229,7 +272,7 @@ class _Transfer:
|
|
|
229
272
|
if getattr(r, "status", 200) != 206 or not str(
|
|
230
273
|
r.headers.get("Content-Range", "")
|
|
231
274
|
).startswith(f"bytes {done}-"):
|
|
232
|
-
raise
|
|
275
|
+
raise NoRangeError()
|
|
233
276
|
read_fn = getattr(r, "read1", r.read)
|
|
234
277
|
with self.io_lock:
|
|
235
278
|
if self.stop.is_set():
|
|
@@ -242,7 +285,7 @@ class _Transfer:
|
|
|
242
285
|
end = segment[1] # a thief may have cut the range short
|
|
243
286
|
if done >= end:
|
|
244
287
|
break
|
|
245
|
-
chunk = read_fn(min(
|
|
288
|
+
chunk = read_fn(min(READ_BYTES, end - done))
|
|
246
289
|
if not chunk:
|
|
247
290
|
break
|
|
248
291
|
with self.io_lock:
|
|
@@ -262,14 +305,14 @@ class _Transfer:
|
|
|
262
305
|
if done < end and not self.stop.is_set():
|
|
263
306
|
raise http.client.IncompleteRead(b"", end - done)
|
|
264
307
|
|
|
265
|
-
def worker(self, segment: list[int], claim:
|
|
308
|
+
def worker(self, segment: list[int], claim: Claim | None = None) -> None:
|
|
266
309
|
if claim is None:
|
|
267
|
-
claim =
|
|
310
|
+
claim = Claim(segment, time.monotonic())
|
|
268
311
|
attempt = 0
|
|
269
312
|
while not self._finished(segment) and not self.stop.is_set():
|
|
270
313
|
try:
|
|
271
314
|
self._range(segment, claim)
|
|
272
|
-
except
|
|
315
|
+
except NoRangeError as exc:
|
|
273
316
|
self.fail(exc)
|
|
274
317
|
except (urllib.error.URLError, OSError, http.client.HTTPException) as exc:
|
|
275
318
|
if self.stop.is_set() or self._finished(segment):
|
|
@@ -303,15 +346,11 @@ def _share(left: int, own: float | None, thief: float | None) -> tuple[int, floa
|
|
|
303
346
|
return 0, float("inf")
|
|
304
347
|
if own is None or thief is None:
|
|
305
348
|
keep = left // 2
|
|
306
|
-
return (
|
|
307
|
-
(keep, left / (own or thief or 1e6))
|
|
308
|
-
if left - keep >= download.STEAL_MIN_BYTES
|
|
309
|
-
else None
|
|
310
|
-
)
|
|
349
|
+
return (keep, left / (own or thief or 1e6)) if left - keep >= STEAL_MIN_BYTES else None
|
|
311
350
|
keep = int(left * own / (own + thief))
|
|
312
|
-
if keep <
|
|
351
|
+
if keep < READ_BYTES:
|
|
313
352
|
keep = 0
|
|
314
|
-
return (keep, left / own) if (left - keep) / thief >=
|
|
353
|
+
return (keep, left / own) if (left - keep) / thief >= STEAL_MIN_S else None
|
|
315
354
|
|
|
316
355
|
|
|
317
356
|
def _close(resp: Any) -> None:
|
|
@@ -332,28 +371,28 @@ def _close(resp: Any) -> None:
|
|
|
332
371
|
threading.Thread(target=close, daemon=True).start()
|
|
333
372
|
|
|
334
373
|
|
|
335
|
-
def
|
|
374
|
+
def finish_transfer(job: Transfer, label: str) -> None:
|
|
336
375
|
if job.errors:
|
|
337
376
|
raise job.errors[0]
|
|
338
377
|
if job.remaining():
|
|
339
378
|
raise RuntimeError(f"{label} download stopped with bytes missing")
|
|
340
379
|
|
|
341
380
|
|
|
342
|
-
def _threads(job:
|
|
381
|
+
def _threads(job: Transfer, ranged: bool) -> list[threading.Thread]:
|
|
343
382
|
if not ranged:
|
|
344
383
|
return [threading.Thread(target=_plain, args=(job,), daemon=True)]
|
|
345
384
|
# a resumed file of one large open range spreads over every connection again
|
|
346
385
|
open_ranges = sum(1 for s in job.segments if s[2] < s[1])
|
|
347
|
-
count = min(job.connections, max(open_ranges, job.remaining() //
|
|
386
|
+
count = min(job.connections, max(open_ranges, job.remaining() // MIN_SEGMENT_BYTES))
|
|
348
387
|
return [
|
|
349
388
|
threading.Thread(target=job.connection, daemon=True)
|
|
350
389
|
for _ in range(max(1, count) if open_ranges else 0)
|
|
351
390
|
]
|
|
352
391
|
|
|
353
392
|
|
|
354
|
-
def
|
|
355
|
-
job:
|
|
356
|
-
source:
|
|
393
|
+
def run_transfer(
|
|
394
|
+
job: Transfer,
|
|
395
|
+
source: remote_ops_common.Source,
|
|
357
396
|
label: str,
|
|
358
397
|
*,
|
|
359
398
|
ranged: bool,
|
|
@@ -372,20 +411,20 @@ def _run(
|
|
|
372
411
|
if not alive or not job.remaining(): # a stalled thief's victim may still be blocked
|
|
373
412
|
break
|
|
374
413
|
for t in alive:
|
|
375
|
-
t.join(timeout=
|
|
414
|
+
t.join(timeout=remote_ops_common.PROGRESS_EVERY_S / len(alive))
|
|
376
415
|
now, done = time.monotonic(), size - job.remaining()
|
|
377
416
|
if allow_slow_switch and not job.stop.is_set():
|
|
378
417
|
window_elapsed = now - slow_window_start
|
|
379
|
-
if window_elapsed >=
|
|
418
|
+
if window_elapsed >= SLOW_WINDOW_S:
|
|
380
419
|
window_rate = int((done - slow_window_done) / max(1e-3, window_elapsed))
|
|
381
420
|
remaining = max(0, size - done)
|
|
382
|
-
near_done = remaining <=
|
|
383
|
-
if window_rate <
|
|
384
|
-
job.fail(
|
|
421
|
+
near_done = remaining <= SLOW_BYTES_PER_S * SLOW_MIN_REMAINING_S
|
|
422
|
+
if window_rate < SLOW_BYTES_PER_S and not near_done:
|
|
423
|
+
job.fail(SlowTransferError(window_rate))
|
|
385
424
|
break
|
|
386
425
|
slow_window_start, slow_window_done = now, done
|
|
387
|
-
if now - mark >=
|
|
388
|
-
|
|
426
|
+
if now - mark >= remote_ops_common.PROGRESS_EVERY_S:
|
|
427
|
+
remote_ops_common.emit(
|
|
389
428
|
"progress",
|
|
390
429
|
phase="download",
|
|
391
430
|
file=label,
|
|
@@ -394,11 +433,11 @@ def _run(
|
|
|
394
433
|
bytes_per_s=meter.add(now, done),
|
|
395
434
|
source=source["source"],
|
|
396
435
|
connections=max(1, job.active()) if ranged else 1,
|
|
397
|
-
**
|
|
436
|
+
**remote_ops_common.step_fields(done),
|
|
398
437
|
)
|
|
399
438
|
mark = now
|
|
400
439
|
if ranged:
|
|
401
|
-
|
|
440
|
+
remote_ops_common.save_ranges(job.part, size, job.snapshot())
|
|
402
441
|
if job.errors or any(t.is_alive() for t in threads):
|
|
403
442
|
job.stop_and_close()
|
|
404
443
|
deadline = time.monotonic() + 2.0
|
|
@@ -406,19 +445,19 @@ def _run(
|
|
|
406
445
|
if t.is_alive():
|
|
407
446
|
t.join(timeout=max(0.0, deadline - time.monotonic()))
|
|
408
447
|
if ranged:
|
|
409
|
-
|
|
448
|
+
remote_ops_common.save_ranges(job.part, size, job.snapshot())
|
|
410
449
|
if job.steals:
|
|
411
|
-
|
|
450
|
+
remote_ops_common.emit(
|
|
412
451
|
"log",
|
|
413
452
|
message=f"{label}: {len(threads)} connections, {job.steals} ranges rebalanced",
|
|
414
453
|
)
|
|
415
454
|
|
|
416
455
|
|
|
417
|
-
def _plain(job:
|
|
456
|
+
def _plain(job: Transfer) -> None:
|
|
418
457
|
"""A single unranged stream (a data: URL or a server without Range support)."""
|
|
419
458
|
segment = job.segments[0]
|
|
420
459
|
try:
|
|
421
|
-
with
|
|
460
|
+
with remote_ops_common.open_url(remote_ops_common.build_request(job.url), timeout=60) as r:
|
|
422
461
|
job._track_response(r)
|
|
423
462
|
try:
|
|
424
463
|
with job.io_lock:
|
|
@@ -428,7 +467,7 @@ def _plain(job: _Transfer) -> None:
|
|
|
428
467
|
with out:
|
|
429
468
|
read_fn = getattr(r, "read1", r.read)
|
|
430
469
|
while not job.stop.is_set():
|
|
431
|
-
chunk = read_fn(
|
|
470
|
+
chunk = read_fn(READ_BYTES)
|
|
432
471
|
if not chunk:
|
|
433
472
|
break
|
|
434
473
|
with job.io_lock:
|