2013-05-03 68 views
3

我有一种情况,我开始tweepy与默认哈希标签流,比程序收到一个队列并更改标签,但我的问题是如何将这个新的标签应用到已经运行的tweepy流?如何更改tweepy中的流如果哈希标签更改?

为了更清楚将举一个例子:

hashtag = 'number_1' 

    auth = t_auth() 
    self.stream = tweepy.streaming.Stream(auth,CustomStreamListener()) 
    self.stream.filter(follow=None, track=[hashtag]) 

确定,上面的代码将侦听主题标签'number_1',现在,假设我接收到的队列具有不同主题标签:

hashtag = 'Different_hashtag' 

那么如何让我的self.stream.filter(follow=None, track=[hashtag]开始聆听新的标签?

+1

您是否尝试调用'self.stream.disconnect()'并再次调用'self.stream.filter(follow = None,track = [hashtag])'?似乎这是唯一的方法。 – alecxe 2013-05-06 10:21:57

+0

听起来很合理,但是,我应该在哪里放?因为一旦我调用'stream.filter()'它会阻塞一切,并且之后的代码将不会被执行。 – Vor 2013-05-06 12:53:43

+0

我想你的'CustomStreamListener'中的某处。选择适当的方法并覆盖它:请参阅https://github.com/tweepy/tweepy/blob/master/tweepy/streaming.py#L20。看起来应该是'on_status'或'on_data'。 – alecxe 2013-05-06 13:52:45

回答

2

由于@alecxe在评论中指出,就可以了,通过调用stream.disconnect(),或返回False

另一种方法是调用stream.filterasync=True断开的回调方法之一的流。这将会异步收听Tweets,当您收到不同的标签时,您可以从主代码中调用stream.disconnect()stream.filter()