1
1
# Django Lightweight Queue
2
2
3
- DLQ is a lightweight & modular queue and cron system for Django.
3
+ DLQ is a lightweight & modular queue and cron system for Django. It powers
4
+ millions of production jobs every day at Thread.
5
+
6
+ ## Installation
7
+
8
+ ``` shell
9
+ pip install django-lightweight-queue[redis]
10
+ ```
11
+
12
+ Currently the only production-ready backends are redis-based, so the ` redis `
13
+ extra is essentially required. Additional non-redis backed production-ready
14
+ backends are great candidates for community contributions.
4
15
5
16
## Basic Usage
6
17
7
- ``` python
18
+ ``` python
8
19
import time
9
20
from django_lightweight_queue import task
10
21
@@ -25,42 +36,50 @@ for more details.
25
36
All automatically picked up configuration options begin with ` LIGHTWEIGHT_QUEUE_ `
26
37
and can be found in ` app_settings.py ` . They should be placed in the usual Django
27
38
settings files, for example:
28
- ``` python
39
+
40
+ ``` python
29
41
LIGHTWEIGHT_QUEUE_BACKEND = ' django_lightweight_queue.backends.redis.RedisBackend'
30
42
```
31
43
32
- If desired, specific configuration overrides can be placed in a standalone python
33
- file which passed on the command line.
44
+ #### Special Configuration
45
+
46
+ If desired, specific configuration overrides can be placed in a standalone
47
+ python file which passed on the command line. This is useful for applying
48
+ customisations for specific servers.
34
49
35
50
For example, given a ` special.py ` containing:
36
- ``` python
51
+
52
+ ``` python
37
53
LIGHTWEIGHT_QUEUE_REDIS_PORT = 12345
38
54
```
55
+
39
56
and then running:
57
+
40
58
```
41
59
$ python manage.py queue_runner --config=special.py
42
60
```
61
+
43
62
will result in the runner to use the settings from the specified configuration
44
63
file in preference to settings from the Django environment. Any settings not
45
64
present in the specified file are inherited from the global configuration.
46
65
47
- ### Backends
66
+ ## Backends
48
67
49
68
There are four built-in backends:
50
- - Synchronous (the default): executes the task inline, without any actual queuing
51
- - Redis: executes tasks at-most-once using [ Redis] [ redis ] for storage of the
52
- enqueued tasks
53
- - Reliable Redis: executes tasks at-least-once using [ Redis] [ redis ] for storage
54
- of the enqueued tasks
55
- - Debug Web: a backend for use in debugging. Instead of running jobs it prints
56
- the url to a view that can be used to run a task in a transaction which will
57
- be rolled back. This is useful for debugging and optimising tasks.
69
+
70
+ | Backend | Type | Description |
71
+ | -------------- | ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
72
+ | Synchronous | Development | Executes the task inline, without any actual queuing. |
73
+ | Redis | Production | Executes tasks at-most-once using [ Redis] [ redis ] for storage of the enqueued tasks. |
74
+ | Reliable Redis | Production | Executes tasks at-least-once using [ Redis] [ redis ] for storage of the enqueued tasks (subject to Redis consistency). Does not guarantee the task _ completes_ . |
75
+ | Debug Web | Debugging | Instead of running jobs it prints the url to a view that can be used to run a task in a transaction which will be rolled back. This is useful for debugging and optimising tasks. |
58
76
59
77
[ redis ] : https://redis.io/
60
78
61
79
## Running Workers
62
80
63
81
The queue runner is implemented as a Django management command:
82
+
64
83
```
65
84
$ python manage.py queue_runner
66
85
```
@@ -73,37 +92,47 @@ $ python manage.py queue_runner --machine 2 --of 4
73
92
```
74
93
75
94
Alternatively a runner can be told explicitly which configuration to use:
95
+
76
96
```
77
97
$ python manage.py queue_runner --exact-configuration --config=special.py
78
98
```
99
+
79
100
When using ` --exact-configuration ` the number of workers is configured exactly,
80
101
rather than being treated as the configuration for a pool. Additionally,
81
102
exactly-configured runners will _ not_ run any cron workers.
82
103
83
- ### Example
104
+ #### Example
84
105
85
106
Given a Django configuration containing:
86
- ``` python
107
+
108
+ ``` python
87
109
LIGHTWEIGHT_QUEUE_WORKERS = {
88
110
' queue1' : 3 ,
89
111
}
90
112
```
113
+
91
114
and a ` special.py ` containing:
92
- ``` python
115
+
116
+ ``` python
93
117
LIGHTWEIGHT_QUEUE_WORKERS = {
94
118
' queue1' : 2 ,
95
119
}
96
120
```
121
+
97
122
Running any of:
123
+
98
124
```
99
125
$ python manage.py queue_runner --machine 1 --of 3 # or,
100
126
$ python manage.py queue_runner --machine 2 --of 3 # or,
101
127
$ python manage.py queue_runner --machine 3 --of 3
102
128
```
129
+
103
130
will result in one worker for ` queue1 ` on the current machine, while:
131
+
104
132
```
105
133
$ python manage.py queue_runner --exact-configuration --config=special.py
106
134
```
135
+
107
136
will result in two workers on the current machine.
108
137
109
138
## Cron Tasks
@@ -115,7 +144,7 @@ To specify that a management command should be run at a given time, place a
115
144
` cron.py ` file in the root folder of the Django app which defines the command
116
145
and which contains a ` CONFIG ` variable:
117
146
118
- ``` python
147
+ ``` python
119
148
CONFIG = (
120
149
{
121
150
' command' : ' my_cron_command' ,
@@ -128,3 +157,9 @@ CONFIG = (
128
157
},
129
158
)
130
159
```
160
+
161
+ ## Maintainers
162
+
163
+ This repository was created by [ Chris Lamb] ( https://github.com/lamby ) at
164
+ [ Thread] ( https://www.thread.com/ ) , and continues to be maintained by the [ Thread
165
+ engineering team] ( https://github.com/thread ) .
0 commit comments