add mechanism to stop loop

This commit is contained in:
chrys 2017-04-23 16:58:32 +02:00
parent 39de89fff5
commit 66a3667141

View File

@ -9,17 +9,18 @@ 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 multiprocessing import Process, Queue
class fenrirEventType(Enum): class fenrirEventType(Enum):
Ignore = 0 Ignore = 0
ScreenUpdate = 1 StopMainLoop = 1
KeyboardInput = 2 ScreenUpdate = 2
BrailleInput = 3 KeyboardInput = 3
PlugInputDevice = 4 BrailleInput = 4
BrailleFlush = 5 PlugInputDevice = 5
ScreenChanged = 6 BrailleFlush = 6
StopMainLoop = 7 ScreenChanged = 7
def __int__(self): def __int__(self):
return self.value return self.value
def __str__(self): def __str__(self):
@ -37,8 +38,10 @@ class eventQueue(Queue):
class eventManager(): class eventManager():
def __init__(self): def __init__(self):
self._mainLoopRunning = True
self._eventThreads = [] self._eventThreads = []
self._eventQueue = eventQueue() self._eventQueue = eventQueue()
self.lock = allocate_lock()
def initialize(self, environment): def initialize(self, environment):
self.env = environment self.env = environment
def shutdown(self): def shutdown(self):
@ -48,22 +51,38 @@ class eventManager():
print(event) print(event)
return(event != fenrirEventType.StopMainLoop) return(event != fenrirEventType.StopMainLoop)
def startMainEventLoop(self): def startMainEventLoop(self):
while(self.proceedEventLoop()): while(True):
pass self.proceedEventLoop()
self.lock.acquire(True)
if not self._mainLoopRunning:
break
self.lock.release()
def stopMainEventLoop(self):
self.lock.acquire(True)
self._mainLoopRunning = False
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 = Thread(target=self.eventWorkerThread, args=(event, function))
self._eventThreads.append(t) self._eventThreads.append(t)
t.start() t.start()
def eventWorkerThread(self, event, function): def eventWorkerThread(self, event, function):
for i in range(20): # for i in range(20):
# while True: while True:
Data = None
try:
Data = function() Data = function()
except Exception as e:
print(e)
self._eventQueue.put({"EVENT":event,"DATA":Data}) self._eventQueue.put({"EVENT":event,"DATA":Data})
self.lock.acquire(True)
if not self._mainLoopRunning:
break
self.lock.release()
def p(): def p():
time.sleep(0.2) time.sleep(0.5)
return("p") #return("p")
e = eventManager() e = eventManager()
@ -71,3 +90,5 @@ 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)
e.addEventThread(fenrirEventType.StopMainLoop,e.stopMainEventLoop)