Home
Categories
Dictionary
Glossary
Download
Project Details
Changes Log
What Links Here
FAQ
License

Second python http tutorial



In the first python http tutorial, we used http communication.

In this tutorial, we will use several Python modules after mashalling / unmarshalling them, and execute them in sub-processes, as explained in Spawning subprocesses in Python http modules.

The code of this tutorial is in the samples/python/sample8 directory.

Overview

In the python http tutorial,the PublishModule module was communicating though http. In this tutorial, we will reimplement the Python module to:
  • Marshall / unsmarshall a Python script twice (though pickle), and execute the two scripts in two sub-processes
  • One of the sub-process will increment each second, but the second one will increment each 500 ms

XML configuration

The XML configuration is not changed compared to the first http tutorial.

Implement the first one second timer

The Python code is different of the Python code used for the first http tutorial because we instead of just starting a timer in the start method we will use a python script which will use the timer.

This script will be marshalled / unsmarshalled through pickle.

Code the python script

The code is very similar to the code of the script in the first http tutorial:
      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")

Code the script which is responsabile to unmarshall the script

The following code will unmarshall the script, and setup the parameters of the included pythonHttpUtils field.
The script must call the setupAfterLoad(self) method of the pythonHttpUtils class. This part is necessary because pickle does not keep the fields belonging to other modules when unmarshalling


      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()    

Put everything together

The start(self) method will be called automatically when starting the framework. This method:
  • Creates an instance of our script which will have a period of one second and a step of 10
  • Creates a sub-process which will use this instance after marhsalling / unmarhsalling with pickle
      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.

Implement the two timers

The next timer is incremeting by 1 each 500 ms. Normally we would just have to perform:
      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)       

See also


Categories: tutorials

Copyright 2017-2020 Dassault Aviation. All Rights Reserved. Documentation and source under the LGPL v3 licence