Twitter streaming API with Python - controlling the process
I'm writing a script that uses Python to consume Twitter's streaming API. My issue is that I would like to collect tweets for only a specific time frame (i.e. 1 hr worth of tweets). I've used a Timer object to s开发者_运维技巧top the process and attempt to close the connection but this works half the time. I also tried to catch KeyboardInterrupt errors and automatically close the connection but this doesn't seem to work either.
My question is: Is there a proper way of dealing with streaming API's and how can I have control over when I want to start and stop consuming the stream? I was thinking about using a child process but am not sure.
Thanks!
urllib2 is a blocking API and includes buffering. There is little you can do to cleanly interrupt its read() at a given time.
You'll need a more flexible HTTP library. Here's a tutorial for Twitter with PyCurl.
精彩评论