split oout processManager

This commit is contained in:
chrys 2017-08-03 22:04:47 +02:00
parent 6c7277ec12
commit cd5e042768
6 changed files with 89 additions and 110 deletions

View File

@ -0,0 +1,26 @@
#!/bin/python
import time
# -*- coding: utf-8 -*-
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
from core import debug
import time
class command():
def __init__(self):
pass
def initialize(self, environment):
self.env = environment
def shutdown(self):
pass
def getDescription(self):
return 'No Description found'
def run(self):
print(time.time())
def setCallback(self, callback):
pass

View File

@ -14,30 +14,14 @@ from ctypes import c_bool
class eventManager(): class eventManager():
def __init__(self): def __init__(self):
self._mainLoopRunning = Value(c_bool, True) self.running = Value(c_bool, True)
self._eventProcesses = []
self._eventQueue = Queue() # multiprocessing.Queue() self._eventQueue = Queue() # multiprocessing.Queue()
self.cleanEventQueue() self.cleanEventQueue()
def initialize(self, environment): def initialize(self, environment):
self.env = environment self.env = environment
self.addSimpleEventThread(fenrirEventType.HeartBeat, self.heartBeatTimer)
def shutdown(self): def shutdown(self):
self.terminateAllProcesses() self.cleanEventQueue()
self.cleanEventQueue()
def heartBeatTimer(self):
try:
time.sleep(0.5)
except:
pass
#self.env['runtime']['settingsManager'].getSettingAsFloat('screen', 'screenUpdateDelay')
return time.time()
def terminateAllProcesses(self):
time.sleep(1)
for proc in self._eventProcesses:
try:
proc.terminate()
except Exception as e:
print(e)
def proceedEventLoop(self): def proceedEventLoop(self):
event = self._eventQueue.get() event = self._eventQueue.get()
st = time.time() st = time.time()
@ -53,7 +37,6 @@ class eventManager():
return return
elif event['Type'] == fenrirEventType.StopMainLoop: elif event['Type'] == fenrirEventType.StopMainLoop:
self.handleStopMainLoop(event) self.handleStopMainLoop(event)
return
elif event['Type'] == fenrirEventType.ScreenUpdate: elif event['Type'] == fenrirEventType.ScreenUpdate:
self.env['runtime']['fenrirManager'].handleScreenUpdate(event) self.env['runtime']['fenrirManager'].handleScreenUpdate(event)
elif event['Type'] == fenrirEventType.KeyboardInput: elif event['Type'] == fenrirEventType.KeyboardInput:
@ -71,36 +54,19 @@ class eventManager():
elif event['Type'] == fenrirEventType.ExecuteCommand: elif event['Type'] == fenrirEventType.ExecuteCommand:
self.env['runtime']['fenrirManager'].handleExecuteCommand(event) self.env['runtime']['fenrirManager'].handleExecuteCommand(event)
def isMainEventLoopRunning(self): def isMainEventLoopRunning(self):
return self._mainLoopRunning.value == 1 return self.running.value == 1
def startMainEventLoop(self): def startMainEventLoop(self):
self._mainLoopRunning.value = 1 self.running.value = 1
while( self.isMainEventLoopRunning()): while( self.isMainEventLoopRunning()):
self.proceedEventLoop() self.proceedEventLoop()
def handleStopMainLoop(self, event): def handleStopMainLoop(self, event):
self._mainLoopRunning.value = 0 self.running.value = 0
time.sleep(0.1) time.sleep(0.1)
def stopMainEventLoop(self, Force = False): def stopMainEventLoop(self, Force = False):
if Force: if Force:
self._mainLoopRunning.value = 0 self.running.value = 0
self._eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None}) self._eventQueue.put({"Type":fenrirEventType.StopMainLoop,"Data":None})
def addCustomEventThread(self, function, pargs = None, multiprocess=False):
self._mainLoopRunning.value = 1
if multiprocess:
t = Process(target=self.customEventWorkerThread, args=(self._eventQueue, function, pargs))
else:# thread not implemented yet
t = Process(target=self.customEventWorkerThread, args=(self._eventQueue, function, pargs))
self._eventProcesses.append(t)
t.start()
def addSimpleEventThread(self, event, function, pargs = None, multiprocess=False, runOnce = False):
self._mainLoopRunning.value = 1
if multiprocess:
t = Process(target=self.simpleEventWorkerThread, args=(event, function, pargs))
self._eventProcesses.append(t)
else:# thread not implemented yet
t = Process(target=self.simpleEventWorkerThread, args=(event, function, pargs))
t.start()
def cleanEventQueue(self): def cleanEventQueue(self):
if self._eventQueue.empty(): if self._eventQueue.empty():
return return
@ -108,40 +74,13 @@ class eventManager():
while True: while True:
self._eventQueue.get_nowait() self._eventQueue.get_nowait()
except Empty: except Empty:
pass pass
def getEventQueue(self):
return self._eventQueue
def getMainLoopRunning(self):
return self.running
def putToEventQueue(self,event, data): def putToEventQueue(self,event, data):
if not isinstance(event, fenrirEventType): if not isinstance(event, fenrirEventType):
return False return False
self._eventQueue.put({"Type":event,"Data":data}) self._eventQueue.put({"Type":event,"Data":data})
return True return True
def customEventWorkerThread(self, eventQueue, function, args):
#if not isinstance(eventQueue, Queue):
# return
if not callable(function):
return
while self.isMainEventLoopRunning():
try:
if args:
function(self._mainLoopRunning, eventQueue, args)
else:
function(self._mainLoopRunning, eventQueue)
except Exception as e:
print(e)
def simpleEventWorkerThread(self, event, function, args, runOnce = False):
if not isinstance(event, fenrirEventType):
return
if not callable(function):
return
while self.isMainEventLoopRunning():
Data = None
try:
if args != None:
Data = function(self._mainLoopRunning, args)
else:
Data = function()
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('eventManager:simpleEventWorkerThread:function():' + st(e),debug.debugLevel.ERROR)
self.putToEventQueue(event, Data)
if runOnce:
break

View File

@ -5,74 +5,86 @@
# By Chrys, Storm Dragon, and contributers. # By Chrys, Storm Dragon, and contributers.
from core import debug from core import debug
from core.eventData import fenrirEventType
import time import time
from threading import Thread
from multiprocessing import Process, Queue from multiprocessing import Process, Queue
from multiprocessing.sharedctypes import Value from multiprocessing.sharedctypes import Value
from ctypes import c_bool
class eventManager(): class processManager():
def __init__(self): def __init__(self):
self._mainLoopRunning = Value(c_bool, True) self._Processes = []
self._eventProcesses = [] self._Threads = []
def initialize(self, environment): def initialize(self, environment):
self.env = environment self.env = environment
self.running = self.env['runtime']['eventManager'].getMainLoopRunning()
self.addSimpleEventThread(fenrirEventType.HeartBeat, self.heartBeatTimer, multiprocess=False)
def shutdown(self): def shutdown(self):
self.terminateAllProcesses() self.terminateAllProcesses()
self.cleanEventQueue() def heartBeatTimer(self, active):
try:
time.sleep(0.5)
except:
pass
return time.time()
def terminateAllProcesses(self): def terminateAllProcesses(self):
time.sleep(1) time.sleep(1)
for proc in self._eventProcesses: for proc in self._Processes:
try: try:
proc.terminate() proc.terminate()
except Exception as e: except Exception as e:
print(e) print(e)
def addCustomEventThread(self, function, pargs = None, multiprocess=False): def addCustomEventThread(self, function, pargs = None, multiprocess = False, runOnce = False):
self._mainLoopRunning.value = 1 eventQueue = self.env['runtime']['eventManager'].getEventQueue()
if multiprocess: if multiprocess:
t = Process(target=self.customEventWorkerThread, args=(self._eventQueue, function, pargs)) t = Process(target=self.customEventWorkerThread, args=(eventQueue, function, pargs, runOnce))
self._Processes.append(t)
else:# thread not implemented yet else:# thread not implemented yet
t = Process(target=self.customEventWorkerThread, args=(self._eventQueue, function, pargs)) t = Thread(target=self.customEventWorkerThread, args=(eventQueue, function, pargs, runOnce))
self._eventProcesses.append(t) self._Threads.append(t)
t.start() t.start()
def addSimpleEventThread(self, event, function, pargs = None, multiprocess=False, runOnce = False): def addSimpleEventThread(self, event, function, pargs = None, multiprocess = False, runOnce = False):
self._mainLoopRunning.value = 1
if multiprocess: if multiprocess:
t = Process(target=self.simpleEventWorkerThread, args=(event, function, pargs)) t = Process(target=self.simpleEventWorkerThread, args=(event, function, pargs, runOnce))
self._eventProcesses.append(t) self._Processes.append(t)
else:# thread not implemented yet else:# thread not implemented yet
t = Process(target=self.simpleEventWorkerThread, args=(event, function, pargs)) t = Thread(target=self.simpleEventWorkerThread, args=(event, function, pargs, runOnce))
self._Threads.append(t)
t.start() t.start()
def customEventWorkerThread(self, eventQueue, function, args): def customEventWorkerThread(self, eventQueue, function, pargs = None, runOnce = False):
#if not isinstance(eventQueue, Queue): #if not isinstance(eventQueue, Queue):
# return # return
if not callable(function): if not callable(function):
return return
while self.isMainEventLoopRunning(): while self.running.value:
try: try:
if args: if pargs:
function(self._mainLoopRunning, eventQueue, args) function(self.running, eventQueue, pargs)
else: else:
function(self._mainLoopRunning, eventQueue) function(self.running, eventQueue)
except Exception as e: except Exception as e:
print(e) print(e)
if runOnce:
break
def simpleEventWorkerThread(self, event, function, args, runOnce = False): def simpleEventWorkerThread(self, event, function, pargs = None, runOnce = False):
if not isinstance(event, fenrirEventType): if not isinstance(event, fenrirEventType):
return return
if not callable(function): if not callable(function):
return return
while self.isMainEventLoopRunning(): while self.running.value:
Data = None Data = None
try: try:
if args != None: if pargs:
Data = function(self._mainLoopRunning, args) Data = function(self.running, pargs)
else: else:
Data = function() Data = function(self.running)
except Exception as e: except Exception as e:
self.env['runtime']['debug'].writeDebugOut('eventManager:simpleEventWorkerThread:function():' + st(e),debug.debugLevel.ERROR) self.env['runtime']['debug'].writeDebugOut('processManager:simpleEventWorkerThread:function():' + str(e),debug.debugLevel.ERROR)
self.putToEventQueue(event, Data) self.env['runtime']['eventManager'].putToEventQueue(event, Data)
if runOnce: if runOnce:
break break

View File

@ -9,6 +9,7 @@ currentdir = os.path.dirname(os.path.realpath(os.path.abspath(inspect.getfile(in
fenrirPath = os.path.dirname(currentdir) fenrirPath = os.path.dirname(currentdir)
from configparser import ConfigParser from configparser import ConfigParser
from core import processManager
from core import eventManager from core import eventManager
from core import inputManager from core import inputManager
from core import outputManager from core import outputManager
@ -318,9 +319,11 @@ class settingsManager():
environment['runtime']['settingsManager'].loadDicts(self.getSetting('general','punctuationProfile')) environment['runtime']['settingsManager'].loadDicts(self.getSetting('general','punctuationProfile'))
if fenrirManager: if fenrirManager:
environment['runtime']['fenrirManager'] = fenrirManager environment['runtime']['fenrirManager'] = fenrirManager
environment['runtime']['eventManager'] = eventManager.eventManager() environment['runtime']['eventManager'] = eventManager.eventManager()
environment['runtime']['eventManager'].initialize(environment) environment['runtime']['eventManager'].initialize(environment)
environment['runtime']['processManager'] = processManager.processManager()
environment['runtime']['processManager'].initialize(environment)
environment['runtime']['inputManager'] = inputManager.inputManager() environment['runtime']['inputManager'] = inputManager.inputManager()
environment['runtime']['inputManager'].initialize(environment) environment['runtime']['inputManager'].initialize(environment)
environment['runtime']['outputManager'] = outputManager.outputManager() environment['runtime']['outputManager'] = outputManager.outputManager()

View File

@ -52,10 +52,10 @@ class driver():
return return
self.updateInputDevices() self.updateInputDevices()
if _udevAvailable: if _udevAvailable:
self.env['runtime']['eventManager'].addCustomEventThread(self.plugInputDeviceWatchdogUdev) self.env['runtime']['processManager'].addCustomEventThread(self.plugInputDeviceWatchdogUdev)
else: else:
self.env['runtime']['eventManager'].addSimpleEventThread(fenrirEventType.PlugInputDevice, self.plugInputDeviceWatchdogTimer) self.env['runtime']['processManager'].addSimpleEventThread(fenrirEventType.PlugInputDevice, self.plugInputDeviceWatchdogTimer)
self.env['runtime']['eventManager'].addSimpleEventThread(fenrirEventType.KeyboardInput, self.inputWatchdog, {'dev':self.iDevicesFD}) self.env['runtime']['processManager'].addSimpleEventThread(fenrirEventType.KeyboardInput, self.inputWatchdog, {'dev':self.iDevicesFD})
def plugInputDeviceWatchdogUdev(self,active , eventQueue): def plugInputDeviceWatchdogUdev(self,active , eventQueue):
context = pyudev.Context() context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context) monitor = pyudev.Monitor.from_netlink(context)
@ -64,12 +64,11 @@ class driver():
while active: while active:
devices = monitor.poll(2) devices = monitor.poll(2)
if devices: if devices:
print('drin')
eventQueue.put({"Type":fenrirEventType.PlugInputDevice,"Data":''}) eventQueue.put({"Type":fenrirEventType.PlugInputDevice,"Data":''})
#self.env['runtime']['settingsManager'].getSettingAsFloat('screen', 'screenUpdateDelay') #self.env['runtime']['settingsManager'].getSettingAsFloat('screen', 'screenUpdateDelay')
return time.time() return time.time()
def plugInputDeviceWatchdogTimer(self): def plugInputDeviceWatchdogTimer(self, active):
time.sleep(2.5) time.sleep(2.5)
return time.time() return time.time()
def shutdown(self): def shutdown(self):

View File

@ -38,7 +38,7 @@ class driver():
self.hichar = None self.hichar = None
def initialize(self, environment): def initialize(self, environment):
self.env = environment self.env = environment
self.env['runtime']['eventManager'].addCustomEventThread(self.updateWatchdog) self.env['runtime']['processManager'].addCustomEventThread(self.updateWatchdog)
def shutdown(self): def shutdown(self):
pass pass
def getCurrScreen(self): def getCurrScreen(self):