A simple Django app to offload tasks from main web server
Project description
Django Leek
The simple and slick way to run async tasks in Django.
- Django-friendly API
- Easy to start and stop
Based on django-queue.
Why?
With a healthy mix of vegetables, such as Celery and Carrot aleady in the midst, what does django-leek
bring?
The most "lightweight" library so far has "install Redis" as step one. Although, Redis is a fantastic software, sometimes you just want a simple way of offload the webserver and run a task async, such as sending an email.
Here django-leek
comes to the rescue. Usage and architecture cannot be simpler, and with so few moving parts, it should be very stable, although it's still not battle tested as e.g. Celery.
With django-leek
you can get up and running quickly The more complex distributed queues can wait until the website has a lot of traffic, and the scalability is really required.
Getting started
-
Install
django-leek
with pip$ pip install django-leek
-
Add
django-leek
toINSTALLED_APPS
in yoursettings.py
file. -
Create tables needed
$ manange.py migrate
-
Make sure the django-leek server is running.
$ python manage.py runleek
-
Go nuts
leek = Leek() @leek.task def send_mail(to): do_what_ever() send_mail.offload(to='foobar@example.com')
You can also use the "old" as found in
django-queue
push_task_to_queue(send_mail, to='foobar@example.com')
-
It's easy to unit test code that in production offloads work to the Leek server.
def _invoke(a_callable, *args, **kwargs):
- a_callable(*args, **kwargs) @patch('django_leek.api.push_task_to_queue', _invoke) def test_mytest(): send_mail.offload(to='sync@leek.com') # now runs synchronously, like a normal function ```
Development
There is a test application you can play around with when developing on django-leek
. Example:
./manage.sh test_app runserver
- Starts the test app./manage.sh test_app leek
- Starts a leek instance for the test app./manage.sh django_leek test
- Run test suite.
Technical overview
In a nutshell, a python SocketServer runs in the background, listening on a tcp socket. SocketServer gets the request to run a task from it's socket, puts the task on a Queue. A Worker thread picks tasks from this Queue, and runs the tasks one by one.
Components
- Python SocketServer that listens to a tcp socket.
- A Worker thread.
- A python Queue
Workflow
The workflow that runs an async task:
- When
SocketServer
starts, it initializes theWorker
thread. SocketServer
listens to requests.- When
SocketServer
receives a request - a callables with args and kwargs - it puts the request on a pythonQueue
. - The
Worker
thread picks a task from theQueue
. - The
Worker
thread runs the task.
Can this queue scale to production?
Depends on the traffic: SocketServer is simple, but solid, and as the site gets more traffic, it's possible to move the django-queue server to another machine, separate database etc. At some point, probably, it's better to pick Celery. Until then, django-leek is a simple, solid, and no-hustle solution.
Settings
To change the default django-queue settings, add a LEEK
dictionary to your project main settings.py
file.
This is the dictionary and the defaults:
LEEK = {
'bind': "localhost:8002",
'host': "localhost",
'port': 8002}
bind
The leek server will bind here.
host
The django server will connect to this host when notifying leek of jobs.
port
The django server will connect to this port when notifying leek of jobs.
Persistence
The following models are used.
QueuedTasks
The model saves every tasks pushed to the queue.
The task is pickled as a tasks_queue.tasks.Task
object, which is a simple class with a callable
,args
and kwargs
attributes, and one method: run()
SuccessTasks
The Worker thread saves to this model the task_id
of every task that was carried out successfuly. task_id is the task's QueuedTasks
id.
FailedTasks
After the Worker tries to run a task and it fails by raising an exception, the Worker saves it to this model. The failed taks is saved by the task_id
, with the exception message. Only the exception from the last run is saved.
Purge Tasks
According to your project needs, you can purge tasks that the Worker completed successfuly.
The SQL to delete these tasks:
DELETE queued,success
FROM tasks_queue_queuedtasks queued
INNER JOIN tasks_queue_successtasks success
ON success.task_id = queued.id;
In a similar way, delete the failed tasks. You can run a cron script, or other script, to purge the tasks.
Authors
Aviah and Samuel Carlsson
See contributors for full list.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-leek-0.9.tar.gz
.
File metadata
- Download URL: django-leek-0.9.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.5.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c1bac3bf2fc2c559ff7598a467655ecea290f9bf2324b36cfaf8f6a065261e8 |
|
MD5 | 628056507a611116263444fdc2b058eb |
|
BLAKE2b-256 | 16b923c2d21537b21c02d4a50bd93697f1f8c4f3e683b5d7195fcac748afbbcb |
File details
Details for the file django_leek-0.9-py3-none-any.whl
.
File metadata
- Download URL: django_leek-0.9-py3-none-any.whl
- Upload date:
- Size: 12.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.5.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c71cdfebc68a983d923a2b731cba7cad24d06b6239287149ba3d403b744f4230 |
|
MD5 | ea444fe123cb019e79a4d9c118b2959c |
|
BLAKE2b-256 | df63c396fe1ab7c68cacafe9a092b61d3917d7fb65013a310f5c845f135206dd |