Multiprocessing Dynamics Script Tutorial (Python 3)
Name | Location |
---|---|
Multi processing | <Vortex Studio Installation Folder>\tutorials\Python\Vortex\PyMultiprocessing\GenerateMechanismWithPy3Multiprocessing.py |
This example shows some basic usage of the Python 3 "multiprocessing" module in a Vortex Dynamics script extension.
With its default settings, it will compute PI to 10000 precision digits and a Fibonacci sequence of 10000 numbers.
Scripts
GenerateMechanismWithPy3Multiprocessing.py
For the dynamics script to be activated. it must be run in the context of a Vortex object, such as a mechanism.
This script will create a mechanism, add a dynamics script extension with the appropriate fields, and referring to Py3Multiprocessing.py.
The mechanism is then saved as Py3MultiprocessingExample.vxmechanism in the working directory.
Py3Multiprocessing.py
The script extension in the mechanism exposes the following inputs:
- VxData::Field<bool> Start → when true, starts the computation
And the following outputs:
- VxData::Field<std::string> PI → the computed value of PI.
- VxData::Field<double> PI_Process_Time → the time it took to compute PI
- VxData::Field<std::string> Fibonacci → the fibonnaci sequence
- VxData::Field<double> Fibonacci_Process_Time → the time it took to comput the sequence
- VxData::Field<std::string> Frame_Counter → the current frame
- VxData::Field<bool> Done → Is set to true when done
To compute PI and fibbonacci, the script's PI() and fib() functions will be called in another python 3 process.
Each step, the script checks is the result is ready in the queue.
When ready, the values and time taken are set in the outputs.
See Writing Python 3 Applications using Vortex Studio for more details about using Python.
Using the tutorial
- The code is fully documented explaining each steps.
- Use python to invoke GenerateMechanismWithPy3Multiprocessing.py
- A mechanism is saved in file Py3MultiprocessingExample.vxmechanism in the working directory.
- Start the Vortex Editor
- Load Py3MultiprocessingExample.vxmechanism
- Select the script in the explorer
- Press Simulate (Play) button to start the simulation.
- In the Properties panel, look at the frame counter output, number should increase
- In the Properties panel, check the "Start" input of the script
- UI stays responsive and the Fibonacci computation is completed before PI is finished being computed.
- The time spent calculating is shown and the Done output is checked when calculations are done