[Answer]-How to repeatedly poll a URL in django?


If all you need to do is send a GET request to a URL, then cron + curl will work for you. Add the following line to your crontab (how to):

* * * * * /usr/bin/curl --silent --compressed http://path.to/the/url

That will poll the URL once every minute, as long as your server is up.

If you want to integrate the polling with Django, check out django-celery, a background task queue for Python and Django. First follow Celery’s Django installation guide, and then take a look at this blog post talking about how to use celery as a cron replacement.

For your use case, you could replace the blog’s example task with

import requests

from celery.task.schedules import crontab
from celery.decorators import periodic_task

@periodic_task(run_every=crontab(hour="*", minute="*", day_of_week="*"))
def test():
    response = requests.get('https://path.to/the/url/')

Leave a comment