runtime/PLCObject.py
changeset 299 d7f8ffe18017
parent 291 701c0601db02
child 301 87c925eaaa3a
equal deleted inserted replaced
298:732e30ac8bf3 299:d7f8ffe18017
   176                     PLCprint("Dirty lib detected :" + badlib)
   176                     PLCprint("Dirty lib detected :" + badlib)
   177                     #dlclose(badhandle)
   177                     #dlclose(badhandle)
   178                     return True
   178                     return True
   179         return False
   179         return False
   180 
   180 
   181     def ExecRuntimePy(self):
   181     def PrepareRuntimePy(self):
   182         self.python_threads_vars = globals().copy()
   182         self.python_threads_vars = globals().copy()
   183         pyfile = os.path.join(self.workingdir, "runtime.py")
   183         pyfile = os.path.join(self.workingdir, "runtime.py")
   184         if os.path.exists(pyfile):
   184         if os.path.exists(pyfile):
   185             # TODO handle exceptions in runtime.py
   185             try:
   186             # pyfile may redefine _runtime_cleanup
   186                 # TODO handle exceptions in runtime.py
   187             # or even call _PythonThreadProc itself.
   187                 # pyfile may redefine _runtime_cleanup
   188             execfile(pyfile, self.python_threads_vars)
   188                 # or even call _PythonThreadProc itself.
       
   189                 execfile(pyfile, self.python_threads_vars)
       
   190             except:
       
   191                 PLCprint(traceback.format_exc())
       
   192 
       
   193     def BeginRuntimePy(self):
       
   194         runtime_begin = self.python_threads_vars.get("_runtime_begin",None)
       
   195         if runtime_begin is not None:
       
   196             runtime_begin()
   189 
   197 
   190     def FinishRuntimePy(self):
   198     def FinishRuntimePy(self):
   191         if self.python_threads_vars.get("_runtime_cleanup",None) is not None:
   199         runtime_cleanup = self.python_threads_vars.get("_runtime_cleanup",None)
   192             self.python_threads_vars["_runtime_cleanup"]()
   200         if runtime_cleanup is not None:
       
   201             runtime_cleanup()
   193         self.python_threads_vars = None
   202         self.python_threads_vars = None
   194 
   203 
   195     def PythonThreadProc(self):
   204     def PythonThreadProc(self):
   196         PLCprint("PythonThreadProc started")
   205         PLCprint("PythonThreadProc started")
       
   206         self.BeginRuntimePy()
   197         res,cmd = "None","None"
   207         res,cmd = "None","None"
   198         while self.PLCStatus == "Started":
   208         while self.PLCStatus == "Started":
   199             #print "_PythonIterator(", res, ")",
   209             #print "_PythonIterator(", res, ")",
   200             cmd = self._PythonIterator(res)
   210             cmd = self._PythonIterator(res)
   201             #print " -> ", cmd
   211             #print " -> ", cmd
   215             if self._LoadNewPLC() and self._startPLC(len(self.argv),c_argv(*self.argv)) == 0:
   225             if self._LoadNewPLC() and self._startPLC(len(self.argv),c_argv(*self.argv)) == 0:
   216                 if debug:
   226                 if debug:
   217                     self._resumeDebug()
   227                     self._resumeDebug()
   218                 self.PLCStatus = "Started"
   228                 self.PLCStatus = "Started"
   219                 self.StatusChange()
   229                 self.StatusChange()
   220                 self.ExecRuntimePy()
   230                 self.PrepareRuntimePy()
   221                 self.PythonThread = Thread(target=self.PythonThreadProc)
   231                 self.PythonThread = Thread(target=self.PythonThreadProc)
   222                 self.PythonThread.start()
   232                 self.PythonThread.start()
   223                 return True
   233                 return True
   224             else:
   234             else:
   225                 PLCprint("Problem starting PLC")
   235                 PLCprint("Problem starting PLC")