This commit is contained in:
Maximilian Giller 2024-01-30 20:08:32 +01:00
parent 44890aced6
commit fc904f42bf

View file

@ -1,6 +1,5 @@
import asyncio import asyncio
from typing import Optional import logging
class ActionQueue: class ActionQueue:
def __init__(self) -> None: def __init__(self) -> None:
@ -8,24 +7,33 @@ class ActionQueue:
self.idle_action: tuple = (None,[],{}) self.idle_action: tuple = (None,[],{})
self.queue_task = asyncio.create_task(self.run_queue()) self.queue_task = asyncio.create_task(self.run_queue())
self.idle_action_task = None self.idle_action_task = None
def __del___(self):
self.queue_task.cancel()
if self.idle_action_task is not None:
self.idle_action_task.cancel()
async def run_queue(self): async def run_queue(self):
# try:
while True: while True:
if self.queued_actions.empty() and self.idle_action[0] is not None: try:
self.idle_action_task = asyncio.create_task(self.idle_action[0](*(self.idle_action[1]), **(self.idle_action[2]))) if self.queued_actions.empty() and self.idle_action[0] is not None:
self.idle_action_task = asyncio.create_task(self.idle_action[0](*(self.idle_action[1]), **(self.idle_action[2])))
action = await self.queued_actions.get()
if self.idle_action_task is not None:
self.idle_action_task.cancel()
self.idle_action_task = None
if action is not None: # If none -> Is idle action update action = await self.queued_actions.get()
await action[0](*(action[1]), **(action[2]))
# except: if self.idle_action_task is not None:
# pass self.idle_action_task.cancel()
self.idle_action_task = None
if action is not None: # If none -> Is idle action update
try:
await action[0](*(action[1]), **(action[2]))
except Exception as ex:
logging.exception("Something went wrong during execution of action.", ex)
except Exception as ex:
logging.exception("Something went wrong in queue task.", ex)
async def add_action_to_queue(self, action, *args, **kwargs): async def add_action_to_queue(self, action, *args, **kwargs):
await self.queued_actions.put((action, args, kwargs)) await self.queued_actions.put((action, args, kwargs))