tasks
In the previous section, we saw that tasks needs to have two parameters: tasks, and hooks:
# modules/hello_world.py
import prism.task
import prism.target
class HelloWorld(prism.task.PrismTask):
def run(self, tasks, hooks):
test_str = "Hello, world!"
return test_str# modules/hello_world.py
from prism.decorators import task
@task()
def hello_world(tasks, hooks):
test_str = "Hello, world!"
return test_strBoth are critical for Prism to run, and Prism will throw an error if it finds a task without both of the parameters.
So what does tasks do? Simply put, it allows users to reference and access other tasks' outputs via the ref(...) function. This is the most important function in Prism. It is impossible to build even moderately complex workflows without it.
Let's say that you want to create a new task that builds off of the "Hello, world!" string created in task HelloWorld above. You could do that as follows:
# modules/second_task.py
import prism.task
import prism.target
class SecondTask(prism.task.PrismTask):
def run(self, tasks, hooks):
hello_world_str = tasks.ref("hello_world")
additional_details = "\n" + "This is a Prism project"!
return hello_world_str + additional_details# modules/second_task.py
import prism.decorators import task
@task()
def second_task(tasks, hooks):
hello_world_str = tasks.ref("hello_world")
additional_details = "\n" + "This is a Prism project"!
return hello_world_str + additional_detailsImportant: in previous versions of Prism,tasks.ref() argument required the .py suffix (e.g., tasks.ref('hello_world.py')). Starting in version 0.2.0rc1, this suffix should be removed from the argument. In other words:
tasks.ref('hello_world.py') --> tasks.ref('hello_world')
If you leave the .py suffix in, then Prism will throw a warning.
Under the hood, Prism does three things:
It parses each file and computes all
tasks.ref(...)calls.Then, it builds a dependency graph based on which files reference others. In the above example, Prism knows that the task in
hello_world.pyshould be run before the task insecond_task.py.It keeps track of the return value for each task and spits out the desired task's return value for each
tasks.ref(...)call.
Last updated