简体   繁体   English

Twitter 流式传输 API 和 Python - 控制过程

[英]Twitter streaming API with Python - controlling the process

I'm writing a script that uses Python to consume Twitter's streaming API.我正在编写一个脚本,它使用 Python 来使用 Twitter 的流媒体 API。 My issue is that I would like to collect tweets for only a specific time frame (ie 1 hr worth of tweets).我的问题是我只想收集特定时间范围内的推文(即 1 小时的推文)。 I've used a Timer object to stop the process and attempt to close the connection but this works half the time.我使用了计时器 object 来停止进程并尝试关闭连接,但这在一半时间内有效。 I also tried to catch KeyboardInterrupt errors and automatically close the connection but this doesn't seem to work either.我还尝试捕获 KeyboardInterrupt 错误并自动关闭连接,但这似乎也不起作用。

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?我的问题是:是否有处理流 API 的正确方法,我如何控制何时开始和停止使用 stream? I was thinking about using a child process but am not sure.我正在考虑使用子进程,但不确定。

Thanks!谢谢!

urllib2 is a blocking API and includes buffering. urllib2 是一个阻塞 API 并且包括缓冲。 There is little you can do to cleanly interrupt its read() at a given time.在给定的时间,您几乎无法完全中断它的 read()。

You'll need a more flexible HTTP library.您将需要更灵活的 HTTP 库。 Here's a tutorial for Twitter with PyCurl .这是Twitter 与 PyCurl的教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM