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

Python http modules service interface



Python http modules have specific options for the service interfaces declaration, allowing to block the Python script on specific services receptions.

Python http modules notifications do not work as for regular Python UDP modules because these modules use http requests, which means that they need to send a GET request to receive a notification from a service.


By default the framework sends the response as soon as the call has been performed by the Python script, and is not blocking after the call, but there are specific options to configure how to block the Python script after the call for:
  • The eventReceived, requestSend and subscribe notifications
  • The eventSend, requestReceived and push invocations

Overview

By default all notify(self, name, instanceId=0) and invoke(self, name) calls are not blocking because the response of the http request is sent back to the Python script as soon as the request has been received, which means that the flow of the Python script will continue even if there is still code to execute in other modules.

The paramaters for the services interfaces in Python http modules allow to block the execution until a service has been effectively notified.

Service interfaces declarations

Notification declaration

All these notifications have:
  • The blocking optional attribute can be set to specify that the Python script must wait after the notify(self, name, instanceId=0) call that the response is received from the framework
  • The waitFor optional element child can be set to specify that the Python script must wait after the notify(self, name, instanceId=0) call that the the service specified in the waitFor element have been received
For example:
      <interfaces>
         <eventReceived service="init" blocking="onChange"/>
         <eventReceived service="tick" blocking="onChange">
            <waitFor service="init" type="onlyOnce" />
         </eventReceived>                   
      </interfaces>

Invocation declaration

All these invocations have:
  • The blocking optional attribute can be set to specify that the Python script must wait after the invoke(self, name) call that the response is received from the framework
  • The waitFor optional element child can be set to specify that the Python script must wait after the invoke(self, name) call that the the service specified in the waitFor element have been received
For example:
      <interfaces>
         <eventReceived service="init" blocking="onChange"/>
         <eventSend service="tick" blocking="onChange">
            <waitFor service="init" type="onlyOnce" />
         <eventSend>                   
      </interfaces>

Blocking attribute values

The blocking optional attribute for a service has the following values:
  • false (the default value): the call is not blocking
  • true or default: the call is blocking. The flow of the Python script execution will be blocked until the service has been received. For this state of the parameter, it will consider that the service has been received at the start of the framework
  • onChange: the call is blocking. The flow of the Python script execution will be blocked until the service has been received. For this state of the parameter, it will consider that the service has not been received at the start of the framework
For example:
      <interfaces>
         <lt;eventReceived service="tick" blocking="onChange" />                 
      </interfaces>

WaitFor element type attribute values

The waitFor optional element has a type attribute which has the following values:
  • onlyOnce (the default value): the flow of the Python script execution will be blocked until the waitFor service has been received. For this state of the parameter, as soon as the waitFor service has been received once, the flow of the Python script execution will not be any more blocked
  • onChange: the flow of the Python script execution will be blocked until the waitFor service has been received. For this state of the parameter, the waitFor service must be received each time after the call to unlock the block
For example:
      <interfaces>
         <eventReceived service="init"/>
         <eventSend service="tick" blocking="onChange">
            <waitFor service="init" type="onlyOnce" />
         <eventSend>                   
      </interfaces>

Example

Suppose this architecture:
pythonnhttpoverview1
  • The userInputs application allows to invoke the tick service
  • The Python http module uses a while loop for each tick service invocation
We have the following services interface declaration:
      <pythonHttpModule name="PublishModule" pythonModuleType="http">
         <pythonImplementation path="pythonAppli" port="8080"/>
         <interfaces>
            <eventReceived service="tick"/>                 
         </interfaces>
      </pythonHttpModule>
We have this code for the Python script:
      from pythonHttpUtils import PythonHttpUtils

         class PythonAppli:
         step = 1

         def start(self):
           while (True): 
             self.pythonHttpUtils.notify("tick")
             self.pythonHttpUtils.echo(self.step)
             self.step = self.step + 1      
By default the loop will execute without waiting for the tick service invocation from the userInput module, which is not what we want. We will see all the steps in a loop even without invoking the tick service once:
pythonnhttpoverview2
The reason is because the response from the GET http request is sent back as soon as the request is received by the framework. We never wait for the tick service invocation to be performed.


The solution is to add a blocking attribute for the tick service interface:
      <pythonHttpModule name="PublishModule" pythonModuleType="http">
         <pythonImplementation path="pythonAppli" port="8080"/>
         <interfaces>
            <eventReceived service="tick" blocking="onChange"/>                 
         </interfaces>
      </pythonHttpModule>
Now the response from the GET http request will be sent back only after the tick service has been notified. The result is that we will have only one step per each tick service invocation:
pythonnhttpoverview3

See also


Categories: concepts | development | python

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