2

I need to run a python script (which is listening to Twitter) which will call various methods on my django app when it gets tweets that match a particular hashtag.

At the moment, I just run the script by hand on the command line but I'd like it to run inside django if possible so that I can control it from there and so it doesn't have to perform HTTP POSTs when it gets new data.

I've looked at celery (briefly) but this seems to be for performing certain small tasks at regular intervals to me.

Is there a way to use celery (or anything else) to be able to control this long-running "listen to Twitter" script that I've got?

bodger
  • 1,112
  • 6
  • 24
  • "performing certain small tasks at regular intervals" seems to be exactly what you're doing here. – Daniel Roseman Aug 22 '12 at 16:59
  • Well the updates to the db that occur when matching tweets are read are indeed small tasks run at regular intervals. But the listening process that listens for tweets is a long-running (permanent) python script - and it's that bit that I'm wondering whether celery can deal with. – bodger Aug 22 '12 at 20:44

1 Answers1

1

You should Supervisord to run your django application and your script. Making the script a part of the Django project, will let you use Django signals which you can use to write a custom signal that will be emitted every time your twitter logic is done doing what it is supposed to. Signals are blocking. If you want them to be asynchronous use Celery with Django

An alternative would be to run your django application and the twitter script via supervisord and then expose a REST API which does a HTTP POST to the Django application. You can use TastyPie for that.

Pratik Mandrekar
  • 9,362
  • 4
  • 45
  • 65
  • I'm already able to update the django app when a tweet arrives using HTTP POST to a URL I've configured on the django side. It's the script that's listening permanently for tweets that I'm trying to integrate into the django app. – bodger Aug 22 '12 at 20:46
  • You could write a custom management command that calls your script and hook it together with `runserver` Ref- http://www.b-list.org/weblog/2008/nov/14/management-commands/ . Also this might be of help http://stackoverflow.com/questions/6532744/can-i-have-some-code-constantly-run-inside-django-like-a-daemon – Pratik Mandrekar Aug 23 '12 at 02:50