Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background processing in Django without Celery

I have a very small part of a Django site that keeps the state of a moderated chat session between two users. Basically, the first user speaks for 3 minutes (and no one else can), then the second user speaks, then a 30 second pause, and the process is repeated one more time. I'm currently using the database and a "RoomState" model to manage the current state of the room (who can speak, etc). State transitions are affected by the client sending a "ping" AJAX POST message every 10 seconds to one of my views, which checks if it's time to change state.

This works, but definitely feels hacky. I was wondering if there was something more lightweight than django-celery + rabbitmq to manage short lived background tasks on a timer. I realize that the nature of the web/Django is stateless, but I just wanted to see if anyone had a simple suggestion to manage the state transitions in a more reliable way.

like image 389
jeffknupp Avatar asked Jan 04 '12 17:01

jeffknupp


People also ask

How do I run a background task in django?

In Django Background Task, all tasks are implemented as functions (or any other callable). There are two parts to using background tasks: creating the task functions and registering them with the scheduler. setup a cron task (or long running process) to execute the tasks.

Should I use Celery with django?

Celery makes it easier to implement the task queues for many workers in a Django application.

What does Celery do in django?

django-celery provides Celery integration for Django; Using the Django ORM and cache backend for storing results, autodiscovery of task modules for applications listed in INSTALLED_APPS, and more. Celery is a task queue/job queue based on distributed message passing.

What does asynchronous mean in django?

Django has support for writing asynchronous (“async”) views, along with an entirely async-enabled request stack if you are running under ASGI. Async views will still work under WSGI, but with performance penalties, and without the ability to have efficient long-running requests.


3 Answers

Author of django-utils here, I'd suggest trying out my newer project Huey -- has richer feature set, better docs, more stable and works with any python framework (including django). Docs.

like image 124
coleifer Avatar answered Oct 12 '22 19:10

coleifer


I know only one alternative to Celery that is more lightweight: Queue in django-utils.

Another way is to use the subprocess module directly but you'll probably have to solve some problems that are already solved in Celery and django-utils.

like image 40
Etienne Avatar answered Oct 12 '22 18:10

Etienne


I'd recommend django-background-tasks. I've used it in several projects and it does really well as a simple task runner. It was also recommended in Two-Scoops of Django.

like image 34
chadgh Avatar answered Oct 12 '22 19:10

chadgh