开发者

Twitter streaming API with Python - controlling the process

开发者 https://www.devze.com 2023-03-10 10:14 出处:网络
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 Tim

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消