You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+58-8Lines changed: 58 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,12 +16,6 @@ A reference implementation and backport of background workers and tasks in Djang
16
16
python -m pip install django-tasks
17
17
```
18
18
19
-
## Usage
20
-
21
-
**Note**: This documentation is still work-in-progress. Further details can also be found on the [DEP](https://github.com/django/deps/blob/main/accepted/0014-background-workers.rst). [The tests](./tests/tests/) are also a good exhaustive reference.
22
-
23
-
### Settings
24
-
25
19
The first step is to add `django_tasks` to your `INSTALLED_APPS`.
26
20
27
21
Secondly, you'll need to configure a backend. This connects the tasks to whatever is going to execute them.
@@ -44,6 +38,10 @@ A few backends are included by default:
44
38
45
39
Note: `DatabaseBackend` additionally requires `django_tasks.backends.database` adding to `INSTALLED_APPS`.
46
40
41
+
## Usage
42
+
43
+
**Note**: This documentation is still work-in-progress. Further details can also be found on the [DEP](https://github.com/django/deps/blob/main/accepted/0014-background-workers.rst). [The tests](./tests/tests/) are also a good exhaustive reference.
44
+
47
45
### Defining tasks
48
46
49
47
A task is created with the `task` decorator.
@@ -83,7 +81,7 @@ The returned `TaskResult` can be interrogated to query the current state of the
83
81
84
82
If the task takes arguments, these can be passed as-is to `enqueue`.
85
83
86
-
### Executing tasks with the database backend
84
+
### The database backend worker
87
85
88
86
First, you'll need to add `django_tasks.backends.database` to `INSTALLED_APPS`, and run `manage.py migrate`.
89
87
@@ -102,6 +100,58 @@ Finally, you can run `manage.py db_worker` to run tasks as they're created. Chec
102
100
> [!CAUTION]
103
101
> The database backend does not work with SQLite when you are running multiple worker processes - tasks may be executed more than once. See [#33](https://github.com/RealOrangeOne/django-tasks/issues/33).
104
102
105
-
### Contributing
103
+
### Retrieving task result
104
+
105
+
When enqueueing a task, you get a `TaskResult`, however it may be useful to retrieve said result from somewhere else (another request, another task etc). This can be done with `get_result` (or `aget_result`):
106
+
107
+
```python
108
+
result_id = result.id
109
+
110
+
# Later, somewhere else...
111
+
calculate_meaning_of_life.get_result(result_id)
112
+
```
113
+
114
+
Only tasks of the same type can be retrieved this way. To retrieve the result of any task, you can call `get_result` on the backend:
115
+
116
+
```python
117
+
from django_tasks import default_task_backend
118
+
119
+
default_task_backend.get_result(result_id)
120
+
```
121
+
122
+
### Return values
123
+
124
+
If your task returns something, it can be retrieved from the `.result` attribute on a `TaskResult`. Accessing this property on an unfinished task (ie not `COMPLETE` or `FAILED`) will raise a `ValueError`.
125
+
126
+
```python
127
+
assert result.status == ResultStatus.COMPLETE
128
+
assert result.result ==42
129
+
```
130
+
131
+
If a result has been updated in the background, you can call `refresh` on it to update its values. Results obtained using `get_result` will always be up-to-date.
132
+
133
+
```python
134
+
assert result.status == ResultStatus.NEW
135
+
result.refresh()
136
+
assert result.status == ResultStatus.COMPLETE
137
+
```
138
+
139
+
### Backend introspecting
140
+
141
+
Because `django-tasks` enables support for multiple different backends, those backends may not support all features, and it can be useful to determine this at runtime to ensure the chosen task queue meets the requirements, or to gracefully degrade functionality if it doesn't.
142
+
143
+
-`supports_defer`: Can tasks be enqueued with the `run_after` attribute?
144
+
-`supports_async_task`: Can coroutines be enqueued?
145
+
-`supports_get_result`: Can results be retrieved after the fact (from **any** thread / process)?
146
+
147
+
```python
148
+
from django_tasks import default_task_backend
149
+
150
+
assert default_task_backend.supports_get_result
151
+
```
152
+
153
+
This is particularly useful in combination with Django's [system check framework](https://docs.djangoproject.com/en/stable/topics/checks/).
154
+
155
+
## Contributing
106
156
107
157
See [CONTRIBUTING.md](./CONTRIBUTING.md) for information on how to contribute.
0 commit comments