Reputation: 18513
I want to call a function from a Django view, that may take 2-5 minutes to finish. I plan to call it asynchronously, return after the job is started, and poll with AJAX to wait for the FINISHED flag.
I don't want to use a heavy-weight solution like Celery+RabbitMQ, just a simple async call is fine.
How should I do it? I have read about subprocesses, threading, and twisted. Which is the best (safest) way to use in a Django view?
Upvotes: 0
Views: 601
Reputation: 11369
If you don't want to resort to Celery+Redis or RabbitMQ, you can write a management command with your processing code. From your view, you can call your management command asynchronously with something like
from subprocess import Popen
p = Popen(['manage.py', 'my_command'])
As someone said on another question: The Popen instance can do various other things like you can poll()
it to see if it is still running.
I hope the popen instance you get is thread safe, or is serializable, so that you can reuse it to know if the process has finished running or not.
Upvotes: 1
Reputation: 19953
The best solution by far is a separate job queuing system such as Celery; there is no particularly good alternative. This is what job queuing systems are for. There are some lighter-weight alternatives to Celery such as RQ (Redis Queue), which uses a Redis instance as the backend and has a handy Django interface called django-rq.
I strongly encourage you to use the specialized tool that solves this problem, because ad hoc solutions tend to cause complex and difficult-to-debug problems.
Upvotes: 4