Skip to content

Commit 698f231

Browse files
committed
Better document existing features
These already existed, just weren't documented
1 parent 4cd363c commit 698f231

File tree

1 file changed

+58
-8
lines changed

1 file changed

+58
-8
lines changed

README.md

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ A reference implementation and backport of background workers and tasks in Djang
1616
python -m pip install django-tasks
1717
```
1818

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-
2519
The first step is to add `django_tasks` to your `INSTALLED_APPS`.
2620

2721
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:
4438

4539
Note: `DatabaseBackend` additionally requires `django_tasks.backends.database` adding to `INSTALLED_APPS`.
4640

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+
4745
### Defining tasks
4846

4947
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
8381

8482
If the task takes arguments, these can be passed as-is to `enqueue`.
8583

86-
### Executing tasks with the database backend
84+
### The database backend worker
8785

8886
First, you'll need to add `django_tasks.backends.database` to `INSTALLED_APPS`, and run `manage.py migrate`.
8987

@@ -102,6 +100,58 @@ Finally, you can run `manage.py db_worker` to run tasks as they're created. Chec
102100
> [!CAUTION]
103101
> 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).
104102
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
106156

107157
See [CONTRIBUTING.md](./CONTRIBUTING.md) for information on how to contribute.

0 commit comments

Comments
 (0)