This tutorial shows how to write a Python task and how to run it as a worker.
Install the Python bridge with the following command:
pip3 install --extra-index-url "https://nexus.decisionbrain.cloud/repository/dbos-pypi/simple" optimserver-workerbridge==3.3.0
The Python bridge is a small library that will help write your task.
Create a Python script for your task, let’s call it
In the beginning of your script, you must instantiate the
ExecutionContext and start it:
from optimserver.workerbridge import ExecutionContext if __name__ == '__main__': execution_context = ExecutionContext() execution_context.start()
Note that calling the
startmethod will make the bridge wait for messages on the standard input of the process. If you need to use the standard input for a debugging purpose for instance, don’t call this method but remember it must be called when the task is executed by the worker shell.
The rest of the code is up to you.
You can use the following methods provided by the
get_all_input_pathsto locate the task input parameter files
get_output_pathto get the path where a particular output parameter file must be saved
add_should_stop_callbackto handle stopping/aborting the task
notify_*to send metrics
For more details, use
help(ExecutionContext) with Python in interactive mode.
For instance, you could have:
number_of_color = execution_context.read_input_str("number_of_color") coloring = compute_coloring(number_of_color) execution_context.write_output_str("coloring", coloring)
compute_coloring is your model function.
This code reads the
number_of_color input parameter and write the result of the task in the
coloring output parameter.
At the end of the script, don’t forget to stop the
You can now test your script from the command line. Make sure to have the following files in the current directory:
kcoloring.py inputs/ └─ number_of_color
Put the value of the
number_of_color input parameter in the
inputs/number_of_color file (as text) and run the command:
Finally, you can get the result of the script in the
Once your Python task is ready, read the page run the worker shell to learn how to configure and run your task as a worker.