Update eventManager.py

This commit is contained in:
chrys87 2017-04-24 10:19:18 +02:00 committed by GitHub
parent ecad919ca5
commit b061773778

View File

@ -5,13 +5,12 @@
# By Chrys, Storm Dragon, and contributers. # By Chrys, Storm Dragon, and contributers.
#from core import debug #from core import debug
#from threading import Thread from queue import Empty
#from queue import Queue, Empty
import time import time
from enum import Enum from enum import Enum
#from _thread import allocate_lock
from multiprocessing import Process, Queue, Lock from multiprocessing import Process, Queue, Lock
from multiprocessing.sharedctypes import Value from multiprocessing.sharedctypes import Value
from ctypes import c_bool
class fenrirEventType(Enum): class fenrirEventType(Enum):
Ignore = 0 Ignore = 0
@ -27,27 +26,18 @@ class fenrirEventType(Enum):
def __str__(self): def __str__(self):
return self.name return self.name
'''
class eventQueue(Queue):
def clear(self):
try:
while True:
self.get_nowait()
except Empty:
pass
'''
class eventManager(): class eventManager():
def __init__(self): def __init__(self):
self._mainLoopRunning = Value('i', 1) self._mainLoopRunning = Value(c_bool, True)
self._eventProcesses = [] self._eventProcesses = []
self._eventQueue = Queue() self._eventQueue = Queue() # multiprocessing.Queue()
self.lock = Lock() self.cleanEventQueue()
def initialize(self, environment): def initialize(self, environment):
self.env = environment self.env = environment
def shutdown(self): def shutdown(self):
self.terminateAllProcesses() self.terminateAllProcesses()
#self._eventQueue.clear() self.cleanEventQueue()
def terminateAllProcesses(self): def terminateAllProcesses(self):
for proc in self._eventProcesses: for proc in self._eventProcesses:
try: try:
@ -58,7 +48,6 @@ class eventManager():
event = self._eventQueue.get() event = self._eventQueue.get()
print(event) print(event)
self.eventDispatcher(event) self.eventDispatcher(event)
def eventDispatcher(self, event): def eventDispatcher(self, event):
if not event: if not event:
return return
@ -68,6 +57,7 @@ class eventManager():
self._mainLoopRunning.value = 0 self._mainLoopRunning.value = 0
return return
elif event['Type'] == fenrirEventType.ScreenUpdate: elif event['Type'] == fenrirEventType.ScreenUpdate:
print('do an update')
pass pass
elif event['Type'] == fenrirEventType.KeyboardInput: elif event['Type'] == fenrirEventType.KeyboardInput:
pass pass
@ -80,39 +70,49 @@ class eventManager():
elif event['Type'] == fenrirEventType.ScreenChanged: elif event['Type'] == fenrirEventType.ScreenChanged:
pass pass
def startMainEventLoop(self): def startMainEventLoop(self):
while(self._mainLoopRunning.value == 1): self._mainLoopRunning.value = True
while(self._mainLoopRunning.value):
self.proceedEventLoop() self.proceedEventLoop()
def stopMainEventLoop(self, Force = False): def stopMainEventLoop(self, Force = False):
if Force: if Force:
self._mainLoopRunning.value = 0 self._mainLoopRunning.value = False
time.sleep(0.5)
self._eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None}) self._eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None})
def addEventThread(self, event, function): def addEventThread(self, event, function):
t = Process(target=self.eventWorkerThread, args=(event, function, self._eventQueue, self.lock)) self._mainLoopRunning.value = True
t = Process(target=self.eventWorkerThread, args=(event, function))
self._eventProcesses.append(t) self._eventProcesses.append(t)
t.start() t.start()
def eventWorkerThread(self, event, function, eventQueue, lock): def cleanEventQueue(self):
if self._eventQueue.empty():
return
try:
while True: while True:
self._eventQueue.get_nowait()
except Empty:
pass
def eventWorkerThread(self, event, function):
while self._mainLoopRunning.value:
Data = None Data = None
try: try:
Data = function() Data = function()
print(Data) print(Data)
except Exception as e: except Exception as e:
print(e) print(e)
eventQueue.put({"Type":event,"Data":Data}) self._eventQueue.put({"Type":event,"Data":Data})
if self._mainLoopRunning.value == 0:
break
def p(): def p():
time.sleep(0.5) time.sleep(0.5)
return("p") return("p")
s = time.time()
e = eventManager() e = eventManager()
e.addEventThread(fenrirEventType.ScreenUpdate,p) e.addEventThread(fenrirEventType.ScreenUpdate,p)
e.addEventThread(fenrirEventType.BrailleInput,p) e.addEventThread(fenrirEventType.BrailleInput,p)
e.addEventThread(fenrirEventType.PlugInputDevice,p) e.addEventThread(fenrirEventType.PlugInputDevice,p)
e.addEventThread(fenrirEventType.ScreenChanged,p) e.addEventThread(fenrirEventType.ScreenChanged,p)
time.sleep(0.5) time.sleep(1.5)
e.addEventThread(fenrirEventType.StopMainLoop,e.stopMainEventLoop) e.addEventThread(fenrirEventType.StopMainLoop,e.stopMainEventLoop)
e.startMainEventLoop()
print(time.time() - s - 1.5)