How to access the Twitter Streaming API from Python?

Why doesn't the Twitter streaming API work with a Python Flask framework?

  • I was able to run it with python but when I try to make use of the same code within flask framework it doesn't work.. Is it because its a streaming api?

  • Answer:

    Based on the code snippet posted in the comments, the problem is the architecture. The Tweepy module - or anything that connects to the Twitter streaming API, or actually anything that has a persistent network connection - is not meant to be called from a HTTP method call. The proper way is to have a separate thread that maintains the persistent connection and runs all the time. The Flask request handlers must only send commands to the thread, such as to send the OAuth credentials; or pick up data that is queued up for the thread.

Miguel Paraz at Quora Visit the source

Was this solution helpful to you?

Other answers

How are you trying to do this in Flask? A code snippet would be really helpful. I'll give this a shot anyway. Results from Twitter's streaming API are 'pushed' from the server. It's not clear from your question, but you're probably not looking at the problem correctly. Flask responds ONCE to a client request (this is just how HTTP works). One request, one response. This means that it's not a very good fit for a streaming API. The whole point of streaming is real-time, but a web server will only respond to a client when a request is made. You could take a look at http://en.wikipedia.org/wiki/Push_technology for a brief overview of some solutions to this problem. Long polling with Flask would be... bad if you're trying to scale in any way at all. Long polling would require that you use something like Tornado (if you have to use Python), but this is where http://nodejs.org would really shine. I mean, just take a look at http://socket.io/. It's like magic!

Jonah Dienye

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.