draft: not polling for admin message anymore (?)

This commit is contained in:
Laureηt 2021-11-14 18:46:07 +01:00
parent e817dffd8d
commit 5ccbd3d8e4
No known key found for this signature in database
GPG key ID: D88C6B294FD40994
2 changed files with 34 additions and 10 deletions

View file

@ -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()

View file

@ -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)