Coverage for src/task/examples.py: 0%
38 statements
« prev ^ index » next coverage.py v7.7.0, created at 2025-04-18 12:36 +0000
« prev ^ index » next coverage.py v7.7.0, created at 2025-04-18 12:36 +0000
1import time
3from task.custom_task import CustomTask
6class BasicTask(CustomTask):
7 """
8 A really basic task to show how you should use the CustomTask object
10 the `_make_subtasks` method accepts both regular functions and nested CustomTasks
11 You can also convert a function to a CustomTask as done here
12 """
14 def do(self):
15 print("Working...")
17 def _make_subtasks(self):
18 return [CustomTask(lambda: print("Working really hard!!!!"))]
21counter = 0
24class RecoverExample(CustomTask):
25 """
26 A simple task that will crash 50% of the time to demonstrate the recovery system
27 """
29 def do(self):
30 print("Working...")
32 def _make_subtasks(self):
33 def raise_an_exc():
34 global counter
35 time.sleep(1)
36 counter += 1
37 if counter % 2 == 1:
38 print("Trying to work...")
39 raise RuntimeError("Error!!!")
40 else:
41 print("I finally could work!!!")
43 class ErrorTask(CustomTask):
44 def _make_subtasks(self):
45 return [raise_an_exc]
47 return [lambda: print("Working really hard!!!!"), ErrorTask()]
50class NestedRecoveryExample(CustomTask):
51 """
52 Here we want to demonstrate that the recovery system will also work using nested CustomTasks
53 """
55 def do(self):
56 print("Working...")
58 def _make_subtasks(self):
59 return [CustomTask(lambda: print("Working really hard!!!!")), RecoverExample()]
62class SimpleMathTask(CustomTask):
63 """
64 In this task, we're basically showing how you can work with task arguments and how to link different subtask together
66 The way it works is kind of logical, when the task is called, the arguments go inside the `do` call, we then send the
67 return value to the next subtask
68 """
70 def do(self, x):
71 return x * 100
73 def _make_subtasks(self):
74 return [lambda x: x + 5, lambda x: x / 100]
77class ComplexMathTask(CustomTask):
78 """
79 This one is just to show that you can link CustomTask to another CustomTask or to a regular function
81 For this task, the schema would be:
82 `ComplexMathTask -> SimpleMathTask -> lambda x*2`
84 The result of the last task will then be returned.
85 """
87 def do(self, x):
88 return x * 0.5
90 def _make_subtasks(self):
91 return [SimpleMathTask, lambda x: x * 2]