Pregunta

I am building a push notification server in Django.

The first step that I want to build is to repeatedly poll a URL of my choosing every 1 minute or so.

I'm having trouble figuring out which tools I need for the job.

I gather that I need Django, one of the Python APNS packages, and what else?

Can I use the requests library (HTTP requests for humans library) to actually do the polling every minute? Or would I need a cron job? I'd appreciate sort of a gameplan. I can't figure out how to call the same URL every minute and have it run in the background.

Thank you!

¿Fue útil?

Solución

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/')
    process(response)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top