Overview
A BRAHMS Python Process is a Python function with structure closely analogous to the event() function of a native BRAHMS Process. The function has the form shown below, and is called by BRAHMS multiple times, with one call being received for each of the framework Events sent by the Engine.
Py Source Code (against 1262)
def brahms_process(persist, input):
...
return (persist, output)
Notes
Inputs and Outputs
The persist variable is initialised by BRAHMS before the first call is made, can be modified by the process during a call, or by BRAHMS between calls, but changes to it persist across calls. This means that the Process can use it to store its state, but means also that the Process should take care to keep the variable clean (particular cases described in the following) to minimise the overhead of passing it in and out of the function. Note that you should not use Python's own "persist" mechanism, since multiple instances of your process may get their data mixed up; the persist mechanism provided by BRAHMS is guaranteed by the framework to be safe in multi-processing environments.
The input variable is initialised anew on each call, and contains information that changes regularly (event data, time data, and input data, specifically).
The output variable is set by the process, and contains the process's response to BRAHMS.
Notes
- You can view the state of any of these variables at any stage during processing simply by printing them using Python syntax, for example
print input .
Function Body
The body of the process function, like that of the event() function in a native Process, is simply a switch statement on the event type, and a set of code sections that handle each different event (example below). The bindings propagate all relevant Process events to the Python function (actually, not everything quite is implemented, but we'll cover those cases in the following).
Example: Hello World
The example below is a functional BRAHMS Python Process. It takes no inputs, and generates no outputs, but executes just fine.
Py Source Code (against 1262)
import copy
import time
def brahms_process (persist , input ):
output = copy .deepcopy (persist ['output'])
if input ['event']['type'] == EVENT_MODULE_INIT:
output ['info']['component'] = (0 , 1 )
output ['event']['response'] = C_OK
elif input ['event']['type'] == EVENT_STATE_SET:
output ['event']['response'] = C_OK
elif input ['event']['type'] == EVENT_INIT_CONNECT:
print 'hello world!'
output ['event']['response'] = C_OK
elif input ['event']['type'] == EVENT_RUN_SERVICE:
print 'tak is still cool at t =', input ['time']['now']
output ['event']['response'] = C_OK
return (persist , output )
|