Friday, October 26, 2018

Re: Trigger actions independently of url request

Celery is probably what you want. It allows you to run scheduled tasks. Alternatively, you can create a custom management command and run that periodically some other way: Cron, etc.

On October 22, 2018 3:11:09 PM CDT, Charley Paulus <charley.paulus@gmail.com> wrote:
Hi Andrew,

Thank you for your answer.

To better clarify my need, here is one use case:
1) I would like my server (where Django is) to ping a remote address (let's say google.com) every 10 minutes, and put the result (communication ok or not) in a Django-generated database.
2) Then I will have a Django view that displays some statistics based on this database records. No worries for this point. My question is strictly on point 1.

So what I need is my server itself to run code on a regular basis.
Can I simply start a thread in manage.py, like here: https://eldarion.com/blog/2013/02/14/entry-point-hook-django-projects/ ?

It seems that Celery and AWS Lambda are external tools that can remotely call my Django API.

I hope the example helps.

Thanks again.

Best regards,
Charley


On Monday, October 22, 2018 at 1:10:52 PM UTC-4, Andrew Pinkham wrote:
On Oct 22, 2018, at 12:08, Charley Paulus <charley...@gmail.com> wrote:
> After reading the Django tutorial, my understanding (I hope I'm wrong) is that the only way to trigger a function of a Django app is when someone on the client side request the url related to that function.

That is correct. A view function/object is only called when a site visitor is routed to that callable by the URL configuration.

> But what if I want the server to run a function indenpendently of a url call, for example:
> - every 10 minutes
> - when a pre-defined combination of variables become true

I'm not clear on your second condition, but you are probably interested in Celery for the first.

http://www.celeryproject.org/

Alternatively, some clouds provide the ability to run code on a regular basis. If your Django code is not closely coupled with cron job, you could use AWS Lambda.

https://docs.aws.amazon.com/lambda/latest/dg/with-scheduled-events.html

If it is tightly coupled and Celery is too powerful for your needs, you could use an AWS Lambda (or Azure Function, etc) to send a request to your Django API on a regular basis.

Andrew
https://jambonsw.com
https://django-unleashed.com

No comments:

Post a Comment