Spaces:
Running
Running
Download integrations/simpy/warehouse.py from pgg-engineering/sincekit: direct link, hf CLI and curl.
- Browser
- Download file 2.74 kB
-
https://huggingface.co/spaces/pgg-engineering/sincekit/resolve/main/integrations/simpy/warehouse.py
- Command line
-
hf download hf://spaces/pgg-engineering/sincekit/integrations/simpy/warehouse.py
-
curl -L -o warehouse.py https://huggingface.co/spaces/pgg-engineering/sincekit/resolve/main/integrations/simpy/warehouse.py
2.74 kB
| """Replicate a simulated robot fleet to dashboards polling at different rates.""" | |
| from dataclasses import dataclass, field | |
| from itertools import islice | |
| import simpy | |
| from sincekit import Index | |
| class Dashboard: | |
| cursor: int = 0 | |
| positions: dict = field(default_factory=dict) | |
| def simulate(robots=256, dashboards=64): | |
| env = simpy.Environment() | |
| index = Index(robots) | |
| positions = {} | |
| readers = [Dashboard() for _ in range(dashboards)] | |
| pending = [] | |
| counts = dict(plans=0, shared_rows=0, applied_rows=0, checked_replicas=0) | |
| def robot(slot): | |
| for step in range(1, 17): | |
| yield env.timeout(1 + slot % 4) | |
| offline = (slot + step) % 7 == 0 | |
| if offline: | |
| positions.pop(slot, None) | |
| else: | |
| positions[slot] = (step, (slot * 3 + step) % 37) | |
| index.write_many([(slot, offline)]) | |
| def poll(reader, phase): | |
| yield env.timeout(phase % 3 / 4) | |
| for _ in range(12): | |
| yield env.timeout(1 + phase % 5) | |
| delivered = env.event() | |
| pending.append((reader, delivered)) | |
| yield delivered | |
| def deliver(group): | |
| plan = index.since_many(reader.cursor for reader in group) | |
| # Fetch each payload once in the same simulation event as the index read. | |
| rows = [(slot, None if deleted else positions[slot]) | |
| for slot, _, deleted in plan.rows] | |
| counts["plans"] += 1 | |
| counts["shared_rows"] += len(rows) | |
| for reader, length in zip(group, plan.prefix_lengths): | |
| for slot, position in islice(rows, length): | |
| if position is None: | |
| reader.positions.pop(slot, None) | |
| else: | |
| reader.positions[slot] = position | |
| if reader.positions != positions: | |
| raise AssertionError("dashboard differs from the authoritative fleet") | |
| reader.cursor = plan.revision | |
| counts["applied_rows"] += length | |
| counts["checked_replicas"] += 1 | |
| def dispatcher(): | |
| while True: | |
| yield env.timeout(1) | |
| if pending: | |
| deliver([reader for reader, _ in pending]) | |
| for _, delivered in pending: | |
| delivered.succeed() | |
| pending.clear() | |
| env.process(dispatcher()) | |
| jobs = [env.process(robot(slot)) for slot in range(robots)] | |
| jobs.extend(env.process(poll(reader, i)) for i, reader in enumerate(readers)) | |
| env.run(until=env.all_of(jobs)) | |
| deliver(readers) # Final catch-up after every agent and polling loop has exited. | |
| return {"revisions": index.revision, **counts} | |
| if __name__ == "__main__": | |
| print(simulate()) | |