From b335b746523ffd59db1402b097a802b3fd99eaac Mon Sep 17 00:00:00 2001 From: rcoh Date: Wed, 8 Dec 2010 16:39:50 -0500 Subject: Code for the demo. Everything works afaik. Contains a couple more optimizations. Contains modify param behavior. Improved support for recursive hooks. Modifications to SmootCoreObject to get us closer to a fully multi-threaded system. This should be the last commit directly to master. All further commits should be on subranches and get merged. --- operationscore/Behavior.py | 2 +- operationscore/Input.py | 15 ++++----------- operationscore/Renderer.py | 2 ++ operationscore/SmootCoreObject.py | 14 +++++++++++--- 4 files changed, 18 insertions(+), 15 deletions(-) (limited to 'operationscore') diff --git a/operationscore/Behavior.py b/operationscore/Behavior.py index 83d2e7d..3bdf1ec 100644 --- a/operationscore/Behavior.py +++ b/operationscore/Behavior.py @@ -41,7 +41,7 @@ class Behavior(SmootCoreObject): [self.addInput(sensorInput) for sensorInput in sensorInputs] else: self.addInput(sensorInputs) - def timeStep(self): + def timeStep(self): #TODO: type checking. clean this up responses = self.processResponse(self.sensorResponseQueue, \ self.recursiveResponseQueue) if type(responses) == type(tuple()) and len(responses) == 2: diff --git a/operationscore/Input.py b/operationscore/Input.py index 9ee59f8..2144678 100644 --- a/operationscore/Input.py +++ b/operationscore/Input.py @@ -1,4 +1,5 @@ import threading,time,Util +from operationscore.SmootCoreObject import * #Abstract class for inputs. Inheriting classes should call "respond" to raise #their event. Inheriting classes MUST define sensingLoop. Called at the #interval specified in RefreshInterval while the input is active. For example, if you are writing @@ -6,7 +7,7 @@ import threading,time,Util #Inheriting classes MAY define inputInit. This is called before the loop #begins. import pdb -class Input(threading.Thread): +class Input(SmootCoreObject): #Event scope is a function pointer the function that will get called when #an Parent is raised. def __init__(self, argDict): @@ -19,16 +20,6 @@ class Input(threading.Thread): self.inputInit() threading.Thread.__init__(self) self.daemon = True #This kills this thread when the main thread stops - #CHEATING until I can get multiple inheritence working - def __setitem__(self,k, item): - self.argDict[k] = item - def __getitem__(self, item): - if item in self.argDict: - return self.argDict[item] - else: - return None - def __getiter__(self): - return self.argDict.__getiter__() def respond(self, eventDict): #if eventDict != []: #pdb.set_trace() @@ -48,7 +39,9 @@ class Input(threading.Thread): def run(self): while self.parentAlive(): time.sleep(self.argDict['RefreshInterval']/float(1000)) + self.acquireLock() self.sensingLoop() + self.releaseLock() def sensingLoop(self): pass def inputInit(self): diff --git a/operationscore/Renderer.py b/operationscore/Renderer.py index 11fd8ca..8e31f8b 100644 --- a/operationscore/Renderer.py +++ b/operationscore/Renderer.py @@ -2,10 +2,12 @@ #Inheriting classes MUST define render which takes a light system and renders it. #Inheriting classes may define initRenderer which is called after the dictionary #is pulled from config. +#TODO: multithreaded-rendering from operationscore.SmootCoreObject import * class Renderer(SmootCoreObject): def init(self): self.initRenderer() + threading.Thread.__init__(self) def render(lightSystem): pass def initRenderer(self): diff --git a/operationscore/SmootCoreObject.py b/operationscore/SmootCoreObject.py index d29e710..8514b3e 100644 --- a/operationscore/SmootCoreObject.py +++ b/operationscore/SmootCoreObject.py @@ -1,16 +1,24 @@ import Util import pdb -class SmootCoreObject: - def __init__(self, argDict): +import threading +import thread +class SmootCoreObject(threading.Thread): + def __init__(self, argDict, skipValidation = False): self.argDict = argDict self.validateArgs(self.className()+'.params') + self.lock = thread.allocate_lock() self.init() #call init of inheriting class # self.__setitem__ = self.argDict.__setitem__ # self.__getitem__ = self.argDict.__getitem__ def init(self): pass + def acquireLock(self): + self.lock = thread.allocate_lock() #TODO: fix. + self.lock.acquire() + def releaseLock(self): + self.lock.release() def className(self): - return str(self.__class__).split('.')[-1] + return str(self.__class__).split('.')[-1] #TODO: this doesn't work. def __setitem__(self,k, item): self.argDict[k] = item def __getitem__(self, item): -- cgit v1.2.3