Download server/design_queue.py from devmandan/qr-scan-difficulty-check: direct link, hf CLI and curl.
- Browser
- Download file 13.2 kB
-
https://huggingface.co/spaces/devmandan/qr-scan-difficulty-check/resolve/main/server/design_queue.py
- Command line
-
hf download hf://spaces/devmandan/qr-scan-difficulty-check/server/design_queue.py
-
curl -L -o design_queue.py https://huggingface.co/spaces/devmandan/qr-scan-difficulty-check/resolve/main/server/design_queue.py
13.2 kB
| """The design test's state (see design.py), in memory only: the worker's one hold, the slot and its upload, the tickets | |
| of pages told busy, and the tests with what they keep for their pages. Pure Python with no I/O: design.py calls it from | |
| the event loop, a test's own thread and the frame builder, and its checks drive it with a fake clock. | |
| The hold is one of: free; a slot, waiting for its upload to start (within hold_s) and arrive; a test in setup; a test | |
| running, scene by scene; a test its page left, stopping after the scene in hand; a ticket's, kept for the oldest live | |
| ticket until its page comes back; or replacing, while a worker that went silent or died is replaced. When it frees | |
| with tickets live it goes to the oldest at once, so pages start in the order they were first told busy. A ticket | |
| lapses when no slot request carrying it comes within its last Retry-After plus ticket_extra_s of its last busy answer. | |
| A test is dropped (its routes answer 404) at the first of: a poll with frames=0 that returns "done"; a poll that | |
| returns "late"; the page's DELETE; idle_s with no request naming it; after_deadline_s past its deadline. Its pictures | |
| go with it, at a poll with frames=0, or when it is late; and past held_max bytes across tests, the pictures of the | |
| finished tests idle longest go first. | |
| """ | |
| from __future__ import annotations | |
| import math | |
| import secrets | |
| import threading | |
| import time | |
| from collections.abc import Callable | |
| from dataclasses import dataclass, field | |
| def new_id() -> str: | |
| """A random 128-bit id, for tests, slots and tickets alike.""" | |
| return secrets.token_hex(16) | |
| class Ticket: | |
| id: str | |
| issued_at: float | |
| last_retry_s: int | |
| last_answer_at: float | |
| class Slot: | |
| id: str | |
| since: float # when its window for an upload to start opened: the grant, or a 415 answer | |
| phase: str = "wait" # wait: no upload yet; up: one is arriving; retry: one more may start, after a 415 | |
| retried: bool = False | |
| class Test: | |
| id: str | |
| slot: str | |
| mode: str | None # "jpeg" or "png" while its page takes pictures; None without them, or from frames=0 on | |
| arrived_at: float | |
| deadline: float | |
| last_request_at: float | |
| state: str = "setup" # setup, running, done or late | |
| done: int = 0 # scenes rendered and read | |
| first_at: float | None = None | |
| counts: dict = field(default_factory=lambda: {"zxing-cpp": [0, 0, 0, 0], "wechat": [0, 0, 0, 0]}) | |
| scenes: dict = field(default_factory=dict) | |
| held: int = 0 # bytes of pictures kept for the page | |
| answers: dict | None = None | |
| ms: int | None = None | |
| facts: dict | None = None | |
| dropped: bool = False | |
| class Queue: | |
| """The hold, the tickets and the tests. cfg: n, deadline_s, hold_s, ticket_extra_s, idle_s, after_deadline_s, | |
| retry_s (lowest, highest), max_tickets and held_max (bytes). Every method takes the lock.""" | |
| def __init__(self, cfg: dict, now: Callable[[], float] = time.monotonic): | |
| self.cfg, self.now, self.lock = cfg, now, threading.RLock() | |
| self.hold: tuple = ("free", None) | |
| self.tickets: dict[str, Ticket] = {} # in the order they were issued | |
| self.tests: dict[str, Test] = {} | |
| self.on = True | |
| def busy(self) -> bool: | |
| with self.lock: | |
| self._tick() | |
| return self.hold[0] != "free" | |
| def release(self) -> None: | |
| """The worker is free: held for the oldest live ticket, if any, else free.""" | |
| with self.lock: | |
| self.hold = ("hold", next(iter(self.tickets.values()))) if self.tickets else ("free", None) | |
| def retry_s(self) -> int: | |
| """The running test's own estimate of what's left, in whole seconds from retry_s's lowest to its highest.""" | |
| with self.lock: | |
| kind, held = self.hold | |
| now, n, c, per = self.now(), self.cfg["n"], self.cfg, 0.5 | |
| if kind in ("running", "stopping") and held.done and held.first_at is not None: | |
| per = (now - held.first_at) / held.done | |
| left = n * per + 1 # free, setup or replacing | |
| if kind == "running": | |
| left = (n - held.done) * per + 1 | |
| elif kind == "stopping": | |
| left = per + 1 | |
| elif kind == "slot" and held.phase != "up": | |
| left += max(0.0, c["hold_s"] - (now - held.since)) | |
| elif kind == "hold": | |
| left += max(0.0, held.last_answer_at + held.last_retry_s + c["ticket_extra_s"] - now) | |
| return int(min(c["retry_s"][1], max(c["retry_s"][0], math.ceil(left)))) | |
| def slot(self, ticket: str | None, readings_wait: bool = False) -> tuple: | |
| """A slot request: ("go", slot, hold_s), ("busy", ticket or None, retry_s) or ("off",).""" | |
| with self.lock: | |
| self._tick() | |
| if not self.on: | |
| return ("off",) | |
| if self.hold[0] == "free" and self.tickets: | |
| self.release() | |
| kind, held = self.hold | |
| now = self.now() | |
| if not readings_wait and (kind == "free" or (kind == "hold" and held.id == ticket)): | |
| if kind == "hold": | |
| del self.tickets[held.id] | |
| s = Slot(new_id(), now) | |
| self.hold = ("slot", s) | |
| return ("go", s.id, self.cfg["hold_s"]) | |
| retry = self.retry_s() | |
| t = self.tickets.get(ticket) if ticket else None | |
| if t is None: | |
| if len(self.tickets) >= self.cfg["max_tickets"]: | |
| return ("busy", None, self.cfg["retry_s"][1]) | |
| t = Ticket(new_id(), now, retry, now) | |
| self.tickets[t.id] = t | |
| t.last_retry_s, t.last_answer_at = retry, now | |
| return ("busy", t.id, retry) | |
| def upload(self, slot: str) -> bool: | |
| """An upload starts under slot: True while the slot is live and no upload of it is under way.""" | |
| with self.lock: | |
| self._tick() | |
| kind, s = self.hold | |
| if kind != "slot" or s.id != slot or s.phase == "up": | |
| return False | |
| s.phase = "up" | |
| return True | |
| def refuse(self, slot: str, retry: bool = False) -> None: | |
| """The upload under slot ended without a test: the slot lapses, or after its first 415 waits once more.""" | |
| with self.lock: | |
| kind, s = self.hold | |
| if kind == "slot" and s.id == slot: | |
| if retry and not s.retried: | |
| s.phase, s.since, s.retried = "retry", self.now(), True | |
| else: | |
| self.release() | |
| def arrive(self, slot: str, mode: str | None) -> Test | None: | |
| """The picture under slot has arrived: its test, in setup; None when the slot went meanwhile.""" | |
| with self.lock: | |
| kind, s = self.hold | |
| if kind != "slot" or s.id != slot or s.phase != "up": | |
| return None | |
| now = self.now() | |
| t = Test(new_id(), slot, mode, now, now + self.cfg["deadline_s"], now) | |
| self.tests[t.id], self.hold = t, ("setup", t) | |
| return t | |
| def started(self, test: Test, facts: dict) -> bool: | |
| """Setup passed: the test runs, unless its page left it meanwhile. Then the caller clears the worker, and only | |
| then frees it (release), so that no next setup reaches the worker before the clear.""" | |
| with self.lock: | |
| if test.dropped: | |
| return False | |
| test.state, test.facts, self.hold = "running", facts, ("running", test) | |
| return True | |
| def no_test(self, test: Test) -> None: | |
| """Setup refused the picture, or couldn't open it: no test, and the worker is free.""" | |
| with self.lock: | |
| self._forget(test) | |
| self.release() | |
| def lose(self, test: Test) -> None: | |
| """The worker went silent or died with this test: it is late (in setup, it never was), and the worker is being | |
| replaced, after which release() follows.""" | |
| with self.lock: | |
| if test.state == "setup": | |
| self._forget(test) | |
| else: | |
| test.state, test.scenes, test.held = "late", {}, 0 | |
| self.hold = ("replacing", None) | |
| def next_scene(self, test: Test) -> tuple[int, str | None] | None: | |
| """The next scene to render, and the pictures it keeps for the page; None once the page has left the test | |
| (the caller then frees the worker).""" | |
| with self.lock: | |
| self._tick() | |
| if test.dropped: | |
| return None | |
| if test.first_at is None: | |
| test.first_at = self.now() | |
| return test.done, test.mode | |
| def scene_done(self, test: Test, s: int, got: dict) -> bool: | |
| """A scene's reads, added to the counts and not kept, and its pictures while the page takes them; True after | |
| the last scene.""" | |
| with self.lock: | |
| for name, (t, d) in got["reads"].items(): | |
| c = test.counts[name] | |
| c[0], c[1], c[2], c[3] = c[0] + int(t and not d), c[1] + int(d and not t), c[2] + int(t), c[3] + int(d) | |
| test.done = s + 1 | |
| if test.mode and not test.dropped and got.get("sha256"): | |
| kept = test.scenes[s] = {k: got.get(k) for k in ("box", "sha256", "jpeg", "png", "patch")} | |
| test.held += sum(len(kept[k] or b"") for k in ("jpeg", "png", "patch")) | |
| self._bound() | |
| return test.done >= self.cfg["n"] | |
| def finish(self, test: Test, answers: dict) -> None: | |
| """After the last scene: the answers, and the worker is free.""" | |
| with self.lock: | |
| test.state, test.answers = "done", answers | |
| test.ms = round(1000 * (self.now() - test.arrived_at)) | |
| self.release() | |
| def poll(self, tid: str, frames0: bool) -> dict | None: | |
| """A poll's answer, or None (404).""" | |
| with self.lock: | |
| self._tick() | |
| t = self.tests.get(tid) | |
| if t is None: | |
| return None | |
| t.last_request_at = self.now() | |
| if frames0: | |
| t.mode, t.scenes, t.held = None, {}, 0 | |
| if t.state == "done": | |
| if frames0: | |
| self._drop(t) | |
| return {"state": "done", "scanners": dict(t.answers), "ms": t.ms} | |
| if t.state == "late": | |
| self._drop(t) | |
| return {"state": "late"} | |
| return {"state": "running", "done": t.done} | |
| def scene(self, tid: str, s: int) -> tuple | None: | |
| """("ready", what scene s keeps, the test's mode), ("wait", None, mode) while it renders, or None (404).""" | |
| with self.lock: | |
| self._tick() | |
| t = self.tests.get(tid) | |
| if t is None or t.mode is None or not 0 <= s < self.cfg["n"]: | |
| return None | |
| t.last_request_at = self.now() | |
| if s in t.scenes: | |
| return "ready", t.scenes[s], t.mode | |
| return ("wait", None, t.mode) if t.state == "running" and s >= t.done else None | |
| def delete(self, any_id: str) -> None: | |
| """The page leaves a test, a slot or a ticket: dropped at once. A slot's id also names the test started under | |
| it, which the page may know by its slot alone: in setup it doesn't start, and running it stops after the scene | |
| in hand.""" | |
| with self.lock: | |
| self._tick() | |
| kind, held = self.hold | |
| t = self.tests.get(any_id) or next((x for x in self.tests.values() if x.slot == any_id), None) | |
| if t is not None: | |
| self._drop(t) | |
| elif kind == "slot" and held.id == any_id: | |
| self.release() | |
| elif self.tickets.pop(any_id, None) is not None and kind == "hold" and held.id == any_id: | |
| self.release() | |
| def _forget(self, t: Test) -> None: | |
| t.dropped, t.scenes, t.held = True, {}, 0 | |
| self.tests.pop(t.id, None) | |
| def _drop(self, t: Test) -> None: | |
| self._forget(t) | |
| if self.hold[0] == "running" and self.hold[1] is t: | |
| self.hold = ("stopping", t) | |
| def _bound(self) -> None: | |
| total = sum(t.held for t in self.tests.values()) | |
| for t in sorted((t for t in self.tests.values() if t.state == "done" and t.held), | |
| key=lambda t: t.last_request_at): | |
| if total <= self.cfg["held_max"]: | |
| return | |
| total, t.scenes, t.held = total - t.held, {}, 0 | |
| def _tick(self) -> None: | |
| now, c = self.now(), self.cfg | |
| kind, held = self.hold | |
| if kind == "slot" and held.phase != "up" and now - held.since > c["hold_s"]: | |
| self.release() | |
| for t in [t for t in self.tickets.values() if now - t.last_answer_at > t.last_retry_s + c["ticket_extra_s"]]: | |
| del self.tickets[t.id] | |
| if self.hold[0] == "hold" and self.hold[1] is t: | |
| self.release() | |
| for t in [t for t in self.tests.values() | |
| if now - t.last_request_at > c["idle_s"] or now > t.deadline + c["after_deadline_s"]]: | |
| self._drop(t) | |