|
1 | 1 | # Build a Python Task Worker
|
2 |
| -## Install the python client |
3 |
| -```shell |
4 |
| - virtualenv conductorclient |
5 |
| - source conductorclient/bin/activate |
6 |
| - cd ../conductor/client/python |
7 |
| - python setup.py install |
8 |
| -``` |
9 |
| - |
10 |
| -## Implement a Task Worker |
11 |
| -[ConductorWorker](https://github.com/Netflix/conductor/blob/main/polyglot-clients/python/conductor/ConductorWorker.py#L36) |
12 |
| -class is used to implement task workers. |
13 |
| -The following script shows how to bring up two task workers named `book_flight` and `book_car`: |
14 |
| - |
15 |
| -```python |
16 |
| -from __future__ import print_function |
17 |
| -from conductor.ConductorWorker import ConductorWorker |
18 |
| - |
19 |
| -def book_flight_task(task): |
20 |
| - return {'status': 'COMPLETED', 'output': {'booking_ref': 2341111, 'airline': 'delta'}, 'logs': ['trying delta', 'skipping aa']} |
21 |
| - |
22 |
| -def book_car_task(task): |
23 |
| - return {'status': 'COMPLETED', 'output': {'booking_ref': "84545fdfd", 'agency': 'hertz'}, 'logs': ['trying hertz']} |
24 |
| - |
25 |
| -def main(): |
26 |
| - print('Starting Travel Booking workflows') |
27 |
| - cc = ConductorWorker('{{ server_host }}{{ api_prefix }}', 1, 0.1) |
28 |
| - cc.start('book_flight', book_flight_task, False) |
29 |
| - cc.start('book_car', book_car_task, True) |
30 |
| - |
31 |
| -if __name__ == '__main__': |
32 |
| - main() |
33 |
| -``` |
34 |
| -### `ConductorWorker` parameters |
35 |
| -```python |
36 |
| -server_url: str |
37 |
| - The url to the server hosting the conductor api. |
38 |
| - Ex: '{{ server_host }}{{ api_prefix }}' |
39 |
| - |
40 |
| -thread_count: int |
41 |
| - The number of threads that will be polling for and |
42 |
| - executing tasks in case of using the start method. |
43 |
| - |
44 |
| -polling_interval: float |
45 |
| - The number of seconds that each worker thread will wait |
46 |
| - between polls to the conductor server. |
47 |
| - |
48 |
| -worker_id: str, optional |
49 |
| - The worker_id of the worker that is going to execute the |
50 |
| - task. For further details, refer to the documentation |
51 |
| - By default, it is set to hostname of the machine |
52 |
| -``` |
53 |
| -### `start` method parameters |
54 |
| -```pythhon |
55 |
| -taskType: str |
56 |
| - The name of the task that the worker is looking to execute |
57 |
| -
|
58 |
| -exec_function: function |
59 |
| - The function that the worker will execute. The function |
60 |
| - must return a dict with the `status`, `output` and `logs` |
61 |
| - keys present. If this is not present, an Exception will be |
62 |
| - raised |
63 |
| -
|
64 |
| -wait: bool |
65 |
| - Whether the worker will block execution of further code. |
66 |
| - Since the workers are being run in daemon threads, when the |
67 |
| - program completes execution, all the threads are destroyed. |
68 |
| - Setting wait to True prevents the program from ending. |
69 |
| - If multiple workers are being called from the same program, |
70 |
| - all but the last start call but have wait set to False. |
71 |
| - The last start call must always set wait to True. If a |
72 |
| - single worker is being called, set wait to True. |
73 |
| -
|
74 |
| -domain: str, optional |
75 |
| - The domain of the task under which the worker will run. For |
76 |
| - further details refer to the conductor server documentation |
77 |
| - By default, it is set to None |
78 |
| -``` |
79 | 2 |
|
80 | 3 | See
|
81 |
| -[https://github.com/Netflix/conductor/tree/main/polyglot-clients/python](https://github.com/Netflix/conductor/tree/main/polyglot-clients/python) |
82 |
| -for the source code. |
| 4 | +[conductor-sdk/conductor-python](https://github.com/conductor-sdk/conductor-python/blob/main/README.md) |
0 commit comments