From d667e8ffd2ebc2b13e9589369f78d2dd37002b65 Mon Sep 17 00:00:00 2001 From: hkc Date: Tue, 9 Jul 2024 19:00:34 +0300 Subject: [PATCH] Added worker display and made interlacing bigger --- swarm/worker.py | 52 ++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/swarm/worker.py b/swarm/worker.py index d3edaef..e3aa776 100644 --- a/swarm/worker.py +++ b/swarm/worker.py @@ -27,15 +27,16 @@ class WorkerManager: self.shmem_name = shmem_name self.base = "https://onemillioncheckboxes.com" self.delay = 0.25 - self.queue: asyncio.Queue[int] = asyncio.Queue(64) + self.queue: asyncio.Queue[int] = asyncio.Queue(128) self.n_toggles = 0 + self.workers: set[tuple[int, int]] = set() async def queue_manager(self): offset = random.randint(0, 999999) while True: - for oy in [0, 4, 2, 6, 1, 3, 5, 7]: - for y in range(oy, 1000, 8): + for oy in [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]: + for y in range(oy, 1000, 16): for x in range(1000): index = (x + y * 1000 + offset) % 1000000 byte, bit = divmod(index, 8) @@ -58,30 +59,32 @@ class WorkerManager: sio = socketio.AsyncClient(http_session=http) async def writer_itself(): - print("Writer running") while not sio.connected: await asyncio.sleep(0.1) - print("Connected and running") - while sio.connected: - index = await self.queue.get() - byte, bit = divmod(index, 8) - mask = 0x80 >> bit - - if self.shmem.buf[OFFSET_AVOID + byte] & mask: - continue - - if (self.shmem.buf[OFFSET_MASK + byte] & mask) == 0: - continue - - if (self.shmem.buf[OFFSET_CANVAS + byte] & mask) != ( - self.shmem.buf[OFFSET_STATE + byte] & mask - ): + cookie = bot_index, random.randint(0, 9999) + self.workers.add(cookie) + try: + while sio.connected: + index = await self.queue.get() byte, bit = divmod(index, 8) - self.n_toggles += 1 - self.shmem.buf[OFFSET_STATE + byte] ^= 0x80 >> bit - await sio.emit("toggle_bit", {"index": index}) - await asyncio.sleep(self.delay) - print("Writer closed") + mask = 0x80 >> bit + + if self.shmem.buf[OFFSET_AVOID + byte] & mask: + continue + + if (self.shmem.buf[OFFSET_MASK + byte] & mask) == 0: + continue + + if (self.shmem.buf[OFFSET_CANVAS + byte] & mask) != ( + self.shmem.buf[OFFSET_STATE + byte] & mask + ): + byte, bit = divmod(index, 8) + self.n_toggles += 1 + self.shmem.buf[OFFSET_STATE + byte] ^= 0x80 >> bit + await sio.emit("toggle_bit", {"index": index}) + await asyncio.sleep(self.delay) + finally: + self.workers.remove(cookie) sio.on("connect", writer_itself) @@ -95,6 +98,7 @@ class WorkerManager: diff = time.time() - last_printout print() + print(f"Workers: {len(self.workers)}") print(f"Queue size: {self.queue.qsize()}/{self.queue.maxsize}") print(f"Toggles: {self.n_toggles / diff:.2f}/s")