|
| 1 | +## Task Options |
| 2 | + |
| 3 | +You can specify additional details about the behavior of a task |
| 4 | +by applying additional options. |
| 5 | + |
| 6 | +There are normally 2 days to do this |
| 7 | + - pass to the `@task` decorator |
| 8 | + - pass to the `.apply_async` method for a one-off use |
| 9 | + |
| 10 | +The structure of use of those options is illustrated below, |
| 11 | +with `options` being keyword-based additional options. |
| 12 | + |
| 13 | +```python |
| 14 | +from dispatcher.publish import task |
| 15 | + |
| 16 | +@task(queue='test_channel', **options) |
| 17 | +def print_hello(): |
| 18 | + print('hello world!!') |
| 19 | +``` |
| 20 | + |
| 21 | +For example, to set a timeout of 1 second on the task: |
| 22 | + |
| 23 | +```python |
| 24 | +from dispatcher.publish import task |
| 25 | + |
| 26 | +@task(queue='test_channel', **options) |
| 27 | +def print_hello(): |
| 28 | + print('hello world!!') |
| 29 | +``` |
| 30 | + |
| 31 | +For the one-off use, `.apply_async` can take options, |
| 32 | +but `.delay` cannot, because of the argument structure. |
| 33 | +Using `.delay` inherently runs the task with the task default options. |
| 34 | + |
| 35 | +```python |
| 36 | +from test_methods import print_hello |
| 37 | + |
| 38 | +print_hello.apply_async(args=[], kwargs={}, **options) |
| 39 | +``` |
| 40 | + |
| 41 | +For the timeout seconds example: |
| 42 | + |
| 43 | +```python |
| 44 | +from test_methods import print_hello |
| 45 | + |
| 46 | +print_hello.apply_async(args=[], kwargs={}, timeout=2) |
| 47 | +``` |
| 48 | + |
| 49 | +The `apply_async` options will take precedence over the |
| 50 | +task default options (those passed into the decorator). |
| 51 | + |
| 52 | +### Task Options Manifest |
| 53 | + |
| 54 | +This section documents specific options. |
| 55 | +These follow a "standard" pattern, meaning that they |
| 56 | +can be used in both of the ways described above. |
| 57 | + |
| 58 | +#### Bind |
| 59 | + |
| 60 | +If `bind=True` is passed (default is `False`), then |
| 61 | +additional argument is inserted at the start of the |
| 62 | +argument list to the method. Like: |
| 63 | + |
| 64 | +```python |
| 65 | +@task(bind=True) |
| 66 | +def hello_world(dispatcher, *args, **kwargs): |
| 67 | + print(f'I see the dispatcher object {dispatcher}') |
| 68 | +``` |
| 69 | + |
| 70 | +The `dispatcher` object contains public methods |
| 71 | +which allow interaction with the parent process. |
| 72 | +Available methods will expand in the future, |
| 73 | +right now it offers: |
| 74 | + |
| 75 | + - `uuid` - the internal id of this task call in dispatcher |
| 76 | + - `worker_id` - the id of the worker running this task |
| 77 | + |
| 78 | +#### Queue |
| 79 | + |
| 80 | +The queue or channel this task is submitted to. |
| 81 | +For instance, the pg_notify channel. |
| 82 | +This can be a callable to get this dynamically. |
| 83 | + |
| 84 | +#### on_duplicate |
| 85 | + |
| 86 | +This option helps to manage capacity, controlling |
| 87 | + - task "shedding" |
| 88 | + - task queuing |
| 89 | + |
| 90 | +Depending on the value, a task submission will be ignored |
| 91 | +if certain conditions are met, "shedding", or queued if all |
| 92 | +workers are busy. |
| 93 | + |
| 94 | + - parallel - multiple tasks (running the given `@task` method) are allowed at the same time. Tasks queue if no free workers are available. |
| 95 | + - discard - if a task is already being ran or is queued, any new submissions of this task are ignored. |
| 96 | + - serial - only 1 task (running the given method) will be ran at a single time in the local dispatcher service. Additional submissions are queued, so all submissions will be ran eventually. |
| 97 | + - queue_one - for idempotent tasks, only 1 task (running the given method) will be ran at a single time, and an additional submission is queued. However, only 1 task will be held in the queue, and additional submissions are discarded. This assures _timely_ running of an idempotent task. |
| 98 | + |
| 99 | +### Unusual Options |
| 100 | + |
| 101 | +These do not follow the standard pattern for some reason. |
| 102 | +Usually for testing. |
| 103 | + |
| 104 | +#### registry |
| 105 | + |
| 106 | +The dispatcher uses a global task registry. |
| 107 | +To enable isolated testing `@task` can take a custom |
| 108 | +(meaning non-global) registry. |
| 109 | + |
| 110 | +There is no real multi-registry feature, |
| 111 | +and additional custom code hooks would be needed to make this work. |
0 commit comments