Python task

This tutorial shows how to write a Python task and how to run it as a worker.

Requirements:

  • Python 3.6+

Steps

  1. 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.

  2. Create a Python script for your task, let’s call it kcoloring.py. 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 start method 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.

  3. The rest of the code is up to you. You can use the following methods provided by the ExecutionContext class:

    • get_input_path or get_all_input_paths to locate the task input parameter files
    • get_output_path to get the path where a particular output parameter file must be saved
    • register a callback with add_should_stop_callback to handle stopping/aborting the task
    • notify_* to send metrics

    For more details, use help(optimserver.workerbridge) and 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)
    

    Where the 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.

  4. At the end of the script, don’t forget to stop the ExecutionContext:

    execution_context.stop()
    
  5. 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:

    python3 kcoloring.py 
    

    Finally, you can get the result of the script in the outputs/coloring file.

  6. 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.