Python task
This tutorial shows how to write a Python task and how to run it as a worker.
Requirements:
Python 3.6+
Steps
Install the Python bridge with the following command:
pip3 install --extra-index-url "https://nexus.decisionbrain.cloud/repository/dbos-pypi/simple" optimserver-workerbridge==4.0.2The Python bridge is a small library that will help write your task.
Create a Python script for your task, let's call it
kcoloring.py. In the beginning of your script, you must instantiate theExecutionContextand 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
ExecutionContextclass:get_input_pathorget_all_input_pathsto locate the task input parameter filesget_output_pathto get the path where a particular output parameter file must be savedregister a callback with
add_should_stop_callbackto handle stopping/aborting the tasknotify_*to send metrics
For more details, use
help(optimserver.workerbridge)andhelp(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)Where the
compute_coloringis your model function.This code reads the
number_of_colorinput parameter and write the result of the task in thecoloringoutput parameter.At the end of the script, don't forget to stop the
ExecutionContext:execution_context.stop()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_colorPut the value of the
number_of_colorinput parameter in theinputs/number_of_colorfile (as text) and run the command:python3 kcoloring.pyFinally, you can get the result of the script in the
outputs/coloringfile.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.