From 5ccbd3d8e424c37c01d932757accc057ba9d71c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Sun, 14 Nov 2021 18:46:07 +0100 Subject: [PATCH] draft: not polling for admin message anymore (?) --- src/emulator.py | 29 +++++++++++++++++++---------- src/utils.py | 15 +++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/emulator.py b/src/emulator.py index 804ba75..54e347c 100644 --- a/src/emulator.py +++ b/src/emulator.py @@ -1,5 +1,6 @@ import asyncio import logging +import threading import time from subprocess import PIPE, Popen # nosec @@ -87,14 +88,18 @@ stream = Popen( ) -async def state_manager(): +def state_manager(loop): + print("ici") ps = r.pubsub() ps.subscribe("admin") while True: - message = ps.get_message(ignore_subscribe_messages=True) - if message is not None: - logging.debug(message) - await asyncio.sleep(5.0) + for message in ps.listen(): + print(message) + asyncio.ensure_future(save(), loop=loop) + + +async def save(): + print("saving") async def emulator(): @@ -118,12 +123,16 @@ async def emulator(): await asyncio.sleep(sleep_t) -async def main(): - task_emulator = asyncio.create_task(emulator()) - task_state_manager = asyncio.create_task(state_manager()) +async def main(loop): + thread = threading.Thread(target=state_manager, args=(loop,)) + thread.start() + + task_emulator = loop.create_task(emulator()) await task_emulator - await task_state_manager + thread.join() if __name__ == "__main__": - asyncio.run(main()) + loop = asyncio.get_event_loop() + loop.run_until_complete(main(loop)) + loop.close() diff --git a/src/utils.py b/src/utils.py index 6823811..79910b7 100644 --- a/src/utils.py +++ b/src/utils.py @@ -67,3 +67,18 @@ class Users(set): """Clear the `has_voted` of each user in the set.""" for user in self: user.has_voted = False + + +# class States(set): +# def save(self, core): +# state = core.save_raw_state() +# with open(f"states/{time.strftime('%Y-%m-%dT%H:%M:%S')}.state", "wb") as state_file: +# for byte in state: +# state_file.write(byte.to_bytes(4, byteorder="big", signed=False)) + +# def load(self, core, state): +# state = ffi.new("unsigned char[397312]") +# with open("states/test.state", "rb") as state_file: +# for i in range(len(state)): +# state[i] = int.from_bytes(state_file.read(4), byteorder="big", signed=False) +# core.load_raw_state(state)