move to process based structure (no GIL), ability to terminate, add dispatcher

This commit is contained in:
chrys 2017-04-24 00:32:55 +02:00
parent 66a3667141
commit 4434e781fc

View File

@ -5,12 +5,13 @@
# 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 threading import Thread
from queue import Queue, Empty #from queue import Queue, Empty
import time import time
from enum import Enum from enum import Enum
from _thread import allocate_lock #from _thread import allocate_lock
#from multiprocessing import Process, Queue from multiprocessing import Process, Queue, Lock
from multiprocessing.sharedctypes import Value
class fenrirEventType(Enum): class fenrirEventType(Enum):
Ignore = 0 Ignore = 0
@ -26,7 +27,7 @@ class fenrirEventType(Enum):
def __str__(self): def __str__(self):
return self.name return self.name
'''
class eventQueue(Queue): class eventQueue(Queue):
def clear(self): def clear(self):
try: try:
@ -34,55 +35,77 @@ class eventQueue(Queue):
self.get_nowait() self.get_nowait()
except Empty: except Empty:
pass pass
'''
class eventManager(): class eventManager():
def __init__(self): def __init__(self):
self._mainLoopRunning = True self._mainLoopRunning = Value('i', 1)
self._eventThreads = [] self._eventProcesses = []
self._eventQueue = eventQueue() self._eventQueue = Queue()
self.lock = allocate_lock() self.lock = Lock()
def initialize(self, environment): def initialize(self, environment):
self.env = environment self.env = environment
def shutdown(self): def shutdown(self):
self._eventQueue.clear() for proc in self._eventProcesses:
try:
proc.terminate()
except Exception as e:
print(e)
#self._eventQueue.clear()
def proceedEventLoop(self): def proceedEventLoop(self):
event = self._eventQueue.get() event = self._eventQueue.get()
self.eventDispatcher(event)
print(event) print(event)
return(event != fenrirEventType.StopMainLoop) return(event['Type'] != fenrirEventType.StopMainLoop)
def eventDispatcher(self, event):
if not event:
return
if event['Type'] == fenrirEventType.Ignore:
pass
elif event['Type'] == fenrirEventType.StopMainLoop:
self._mainLoopRunning.value = 0
elif event['Type'] == fenrirEventType.ScreenUpdate:
pass
elif event['Type'] == fenrirEventType.KeyboardInput:
pass
elif event['Type'] == fenrirEventType.BrailleInput:
pass
elif event['Type'] == fenrirEventType.PlugInputDevice:
pass
elif event['Type'] == fenrirEventType.BrailleFlush:
pass
elif event['Type'] == fenrirEventType.ScreenChanged:
pass
def startMainEventLoop(self): def startMainEventLoop(self):
while(True): while(True):
self.proceedEventLoop() if not self.proceedEventLoop():
self.lock.acquire(True) self._mainLoopRunning.value = 0
if not self._mainLoopRunning: break
break def stopMainEventLoop(self, Force = False):
self.lock.release() if Force:
def stopMainEventLoop(self): self._mainLoopRunning.value = 0
self.lock.acquire(True) time.sleep(0.5)
self._mainLoopRunning = False self._eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None})
self.lock.release()
self._eventQueue.put({"EVENT":fenrirEventType.StopMainLoop,"DATA":None})
def addEventThread(self, event, function): def addEventThread(self, event, function):
t = Thread(target=self.eventWorkerThread, args=(event, function)) t = Process(target=self.eventWorkerThread, args=(event, function, self._eventQueue, self.lock))
self._eventThreads.append(t) self._eventProcesses.append(t)
t.start() t.start()
def eventWorkerThread(self, event, function): def eventWorkerThread(self, event, function, eventQueue, lock):
# for i in range(20):
while True: while True:
Data = None Data = None
try: try:
Data = function() Data = function()
print(Data)
except Exception as e: except Exception as e:
print(e) print(e)
self._eventQueue.put({"EVENT":event,"DATA":Data}) eventQueue.put({"Type":event,"Data":Data})
self.lock.acquire(True) if self._mainLoopRunning.value == 0:
if not self._mainLoopRunning:
break break
self.lock.release()
def p(): def p():
time.sleep(0.5) time.sleep(0.5)
#return("p") return("p")
e = eventManager() e = eventManager()