Reputation: 7747
I'm trying to create a function that, when called, will extract information from an external source at irregular (and undefined) intervals. This data will then be placed in a database for later retrieval. I want this to be then running in the background even as other page requests are made. Is this possible?
Upvotes: 2
Views: 1342
Reputation: 9568
Since you seem to need the function to be called when a page is loaded, you can put it inside your view as
def my_view(request):
#Call the long running function
long_running_function()
#Do view logic and return
return HttpResponse(...)
To handle the long_running_function
you could use celery
and create a tasks.py
which implements your external data source logic. Creating tasks, adding to the queue and configuring celery
is summarized here
If you just need a simpler solution for trying it out, take a look at the subprocess module.
A very similar answer here Django: start a process in a background thread?
Upvotes: 0
Reputation: 9262
The best way to run a Django function outside the request/response cycle is to implement it as a custom management command, which you can then set to run periodically using cron
.
If you're already using it, celery
supports periodic tasks using celerybeat
, but this requires configuring and running the celerybeat
daemon, which can be a headache. Celery also supports long-running tasks (things started in a view, but completing in their own time), as described in your question title.
Upvotes: 1