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

Spawning subprocesses in Python http modules



By default Python http modules are working as Python UDP modules, but the associated Python script communicate with the framework using http requests.

However, it is possible to use a Python module spawning several sub-processes, all communicating with the framework.

Overview

The steps to spawn a sub-process is the following:
  • The Python script call the createSubProcess(self, instance, scriptName, fileName, wait=True) method of the pythonHttpUtils class and give the following arguments:
    • instance: An instance of any class which will be used for the http communication with the framework, and which will also be responsible for all the functionality of the spawned instance
    • scriptName: A script which will be used to unmarshall the instance after creating the sub-process
    • fileName: the name of the TEMP file which will be use to marshall the instance
    • wait (optional, True by default): specifies if the . You should set it to False if you want to span more than one process
  • The pythonHttpUtils class marshall the instance in the provided TEMP file using pickle, and create a sub-process with the provided script
  • The script must use unmarshall the instance using pickle
  • Then 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

pythonsubprocess

Parameters of the Popen command

The arguments of the Popen command are:
  • 1: The TEMP file, after the instance has been marshalled
  • 2 to n: The content of the services definition used by the pythonHttpUtils class. ThesetupAfterLoad(self) method will use these arguments to recreate the content of the services map in the pythonHttpUtils instance after unmarshalling

Example

In the Python script, the following code will marshall the PythonInstance and create the sub process with Popen:
      from pythonHttpUtils import PythonHttpUtils
      from pyAppClass import PythonInstance

      class PythonAppli:

         def start(self):
           instance = PythonInstance(self.pythonHttpUtils)
           p = self.pythonHttpUtils.createSubProcess(instance, 'pyAppScript.py', 'app.pkl')      
The pyAppScript.py script has the following code:
      import pickle, sys

      # Get the adress of the serialized app class and unmarshall it
      strAppFile = sys.argv[1]
      app = pickle.load(open(strAppFile, 'rb'))
      # setup the parameter of the internal pythonHttpUtils field
      app.pythonHttpUtils.setupAfterLoad()
      # there the app is completely functional and can communicate with the framework

See also


Categories: concepts | python

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