Return Value From Task

Problem

You want to return a task result to the client.

Solution

Set the task’s result property with a user defined result object. The task’s result property is serialized and returned back to the client. The client can access the task’s result using the result property of the task after the task completes.

 1from agiparallel.client import *
 2from agiparallel.infrastructure import *
 3
 4
 5class returns_simple_value(object):
 6    def execute(self):
 7        self.result = 3
 8
 9
10class sets_value_of_properties(object):
11    def execute(self):
12        self.set_property("A", 1)
13        self.set_property("B", "2")
14        self.set_property("C", 3.14)
15
16
17if __name__ == "__main__":
18    with ClusterJobScheduler("localhost") as client:
19        client.connect()
20        job = client.create_job()
21        job.add_task(returns_simple_value())
22        job.add_task(sets_value_of_properties())
23        job.submit()
24        job.wait_until_done()
25        print(f"Simple value: result={job.tasks[0].result}")
26        print(f'Sets property: A={job.tasks[1].properties["A"]}, B={job.tasks[1].properties["B"]}, C={job.tasks[1].properties["C"]}')

Discussion

Returning a value after the task executes is necessary if the result needs processing. A task can return different types of results including user defined objects. To learn more about all of these options, read Working With Task Results.

See Also