4 Execution of a calculation scheme with the Python programming interface
5 ==========================================================================
6 The first step is to have a Python calculation scheme object that will be called p.
7 It is obtained using :ref:`schemapy`.
9 If it has not already been done, YACS modules have to be imported and YACS needs to be initialised
10 for SALOME. An Executor object will then have to be created that will execute the calculation scheme.
12 Create the Executor object::
14 e = pilot.ExecutorSwig()
16 There are two modes by which a scheme may be executed:
18 - standard mode that executes the scheme in block, waiting until the execution is completely finished
19 - step by step mode, to partially execute the scheme and make pauses and restarts.
21 Executing the scheme in standard mode
22 ----------------------------------------
23 The Executor RunW method is used for this type of execution. This method has three arguments.
24 The first is the calculation scheme (or even a composite node under some conditions). The second indicates
25 the verbosity for execution in debug mode (0 is the level without debug, 1, 2 or 3 for debug).
26 The third is a boolean that is equal to True if execution starts from zero (all nodes are calculated) or False if
27 execution starts from a backed up state of the scheme.
28 The default values of the latter two arguments are equal to 0 and True.
30 Execution of the scheme by the Executor object starting from zero, without debug::
33 if p.getEffectiveState() != pilot.DONE:
34 print p.getErrorReport()
37 If the execution took place correctly, the state of the scheme obtained by ``p.getEffectiveState()`` is equal to DONE.
38 If this is not the case, the scheme is in error. An error report (see :ref:`errorreport`) is given by ``p.getErrorReport()``.
40 In all cases, the values contained in the input and output ports can then be retrieved.
42 Saving the state of a scheme
43 ------------------------------------
44 The state of a scheme after it has been executed can be saved in an XML file for a later restart if required.
45 This save is done using the saveState method for the calculation scheme. It uses an argument that is the name of
46 the file in which the state will be saved.
48 This call is in the following format::
50 p.saveState("state.xml")
52 Loading the state of a scheme
53 ------------------------------------
54 A calculation scheme can be initialised with a state saved in a file. Obviously, the calculation scheme must exist.
55 Then, the loadState function of the loader module must be used. It takes two arguments: the calculation scheme and
56 the name of the file containing the saved state.
58 For example, proceed as follows to initialise scheme p with the previously saved state::
61 loader.loadState(p,"state.xml")
63 This initialisation is only possible if the structures of the scheme and the saved state are identical.
65 After initialisation, execution is started by transferring False as a third argument of RunW::
69 Executing the scheme in step by step mode
70 -----------------------------------------------
71 This type of execution is for advanced users because it requires the use of thread programming in Python.
72 Do not forget to import the threading module::
76 If it is required to use step by step mode or to add breakpoints, the scheme will have to be executed in a
77 separate thread and execution control operations will have to be done in the main thread.
79 Execution with stop on breakpoints
80 +++++++++++++++++++++++++++++++++++++++
81 Before execution with breakpoints is possible, it is necessary to define the list of breakpoints, to set
82 execution mode to execution with breakpoints, and then to start execution in a separate thread.
84 The list of breakpoints is defined by transferring a list of node names to the executor setListOfBreakPoints method.
85 The mode is defined using the executor setExecMode method with a parameter equal to 2.
88 - 0, for standard execution mode (CONTINUOUS mode)
89 - 1, for execution mode with pause at each execution step (STEPBYSTEP mode)
90 - 2, for execution mode with breakpoints (STOPBEFORENODES mode).
92 Execution is started using the executor RunB method instead of RunW for a standard execution.
93 The three arguments are the same:
95 - the calculation scheme
96 - the display level (0,1,2,3)
97 - execution start from zero (True, False)
99 For example, the following will be written to stop before node “node1”::
101 e.setListOfBreakPoints(['node1'])
103 mythread=threading.Thread(target=e.RunB, args=(p,1))
106 The state of nodes in the scheme can be displayed by calling the displayDot method during a pause::
110 It is then necessary to wait until the execution changes to pause: an execution changes to pause when
111 all possible tasks before execution of node “node1” are terminated. The waitPause method is used to
112 control this wait. The next step will be to stop execution by calling the stopExecution method and the
113 thread will then be released. It is recommended that there should be a short wait before waiting for the pause.
114 In total, the following sequence will be added::
121 A stop on breakpoint followed by resuming until the end of the scheme
122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
123 After a stop on breakpoint, an execution is resumed by changing to CONTINUE mode and then restarting
124 the execution by calling the resumeCurrentBreakPoint method.
125 The wait for the end of scheme execution is controlled by calling waitPause.
127 The call sequence to resume on breakpoint is as follows::
130 e.resumeCurrentBreakPoint()
135 Execution in step by step mode
136 +++++++++++++++++++++++++++++++++++++
137 When executing in step by step mode, the calculation scheme executor pauses at each execution step.
138 An execution step corresponds to execution of a group of calculation nodes. This group may contain
139 more than one calculation node if the calculation scheme contains parallel execution branches.
141 This mode is selected by calling the setExecMode method using the value 1 (STEPBYSTEP mode), and the executor
142 is then started in a thread in the same way as above. The waitPause method is used to wait until the end of
143 the execution step. The list of nodes in the next step is obtained using the getTasksToLoad method.
144 The list of nodes to be executed is defined using the setStepsToExecute method and the execution is
145 resumed using the resumeCurrentBreakPoint method.
146 Everything must be placed in a loop that is exited at the end of the execution when there are no longer any nodes to be executed.
148 The complete sequence is as follows::
151 mythread=threading.Thread(target=e.RunB, args=(p,0))
158 bp=e.getTasksToLoad()
160 e.setStepsToExecute(bp)
163 e.resumeCurrentBreakPoint()