PublishModule
module was communicating though http. In this tutorial, we will reimplement the Python module to:pickle
), and execute the two scripts in two sub-processesstart
method we will use a python script which will use the timer.from pythonHttpUtils import PythonHttpUtils class PythonInstance: step = 1 stepBase = 1 instanceId = 0 def __init__(self, thePythonUtils, period, step): self.pythonHttpUtils = thePythonUtils self.stepBase = step self.step = step self.period = period def startTimer(self): timerName = 'timer' + str(self.step) # the timer will elapse every period seconds and call the "tick" method self.pythonHttpUtils.startTimer(timerName, self.period, self.tick) def subscribe(self, serviceName): eventService = self.pythonHttpUtils.getService(serviceName); eventValue = eventService["event"] if eventValue: self.step = -1 * self.stepBase else: self.step = self.stepBase def tick(self): # get the step value self.pythonHttpUtils.notify("event", self.instanceId) self.subscribe("event") # get the last value of the tick self.pythonHttpUtils.notify("published", self.instanceId) publishService = self.pythonHttpUtils.getService("published"); count = publishService['value'] # increment the tick count = count + self.step # invoke the "published" service self.pythonHttpUtils.setValue(publishService, "value", count) r1 = self.pythonHttpUtils.invoke("published")
pythonHttpUtils
field.import pickle, sys, traceback # Get the adress of the serialized app class and unmarshall it strAppFile = sys.argv[1] app = pickle.load(open(strAppFile, 'rb')) app.pythonHttpUtils.setupAfterLoad() app.startTimer()
start(self)
method will be called automatically when starting the framework. This method:from pythonHttpUtils import PythonHttpUtils from pyAppClass import PythonInstance class PythonAppli: def start(self): instance = PythonInstance(self.pythonHttpUtils, 1, 10) p = self.pythonHttpUtils.createSubProcess(instance, 'pyAppScript.py', 'app.pkl')The timer will be updated by one each second.
from pythonHttpUtils import PythonHttpUtils from pyAppClass import PythonInstance class PythonAppli: def start(self): instance = PythonInstance(self.pythonHttpUtils, 1, 10) p = self.pythonHttpUtils.createSubProcess(instance, 'pyAppScript.py', 'app.pkl') instance = PythonInstance(self.pythonHttpUtils, 0.5, 1) p1 = self.pythonHttpUtils.createSubProcess(instance1, 'pyAppScript.py', 'app2.pkl')However if we start the framework with this code, only the first timer is working. This is due to the fact that the main script is waiting for the process to return by default m(meaning that the second sub-process will never be called). We should do instead:
from pythonHttpUtils import PythonHttpUtils from pyAppClass import PythonInstance class PythonAppli: def start(self): instance = PythonInstance(self.pythonHttpUtils, 1, 10) p = self.pythonHttpUtils.createSubProcess(instance, 'pyAppScript.py', 'app.pkl', False) instance = PythonInstance(self.pythonHttpUtils, 0.5, 1) p1 = self.pythonHttpUtils.createSubProcess(instance1, 'pyAppScript.py', 'app2.pkl', False)
Copyright 2017-2020 Dassault Aviation. All Rights Reserved. Documentation and source under the LGPL v3 licence