【问题标题】:Why is there a delay when streaming tweets live?为什么直播推文时会有延迟?
【发布时间】:2021-01-08 19:46:31
【问题描述】:

我正在尝试通过来自特定用户的 tweepy 流通过流获取实时推文数据,但是我发现从发布推文的确切时间戳和来自我的 tweepy 程序的打印文本的时间戳之间恰好有 4 秒的延迟.这是正常/预期的还是有办法让我的代码更有效率?谢谢!

# # # # TWITTER STREAMER # # # #
class TwitterStreamer():
    """
    Class for streaming and processing live tweets.
    """
    def __init__(self):
        pass


    def stream_tweets(self):
        # This handles Twitter authetification and the connection to Twitter Streaming API
        listener = TweetListener()
        auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
        auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
        stream = Stream(auth, listener)

        # This line filter Twitter Streams to capture data by the keywords: 
        stream.filter(follow=['user_id'])


# # # # TWITTER STREAM LISTENER # # # #
class TweetListener(StreamListener):
    
    #This is a basic listener that just prints received tweets 
    
     #Only returns the tweets of given user
    def on_status(self, status):
        if status.user.id_str != 'user_id':
            return
        print(status.text)

    def on_data(self, data):
        try:
            json_load = json.loads(data) 
            text = json_load['text']
            if 'RT @' not in text:
                print(text)
                print(datetime.now()) 
            return True
        except BaseException as e:
            print("Error on_data %s" % str(e))
        return True
          

    def on_error(self, status):
        print(status)

if __name__ == '__main__':
    
    streamer=TwitterStreamer()
    streamer.stream_tweets()

【问题讨论】:

    标签: python twitter tweepy tweets


    【解决方案1】:

    这或多或少会是真的,是的。延迟取决于网络连接和位置等许多因素,但通常我预计会有几秒钟的延迟。

    【讨论】:

    • 啊,好吧。只是发现每次延迟正好是 4 秒很奇怪,所以认为它可能与我的代码有关。谢谢!
    • 你认为使用 TwitterAPI 或 twython 而不是 tweepy 延迟会更好吗?
    • 不太可能,如果它很常见的话,它更有可能是与网络相关的问题。
    • 我可以通过在离 Twitter 服务器更近的机器上运行程序来缓解这种情况吗?
    • 嘿,马特!我正在做类似的事情并得到类似的 3/4 秒延迟...您最后设法减少延迟吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 2022-08-20
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多