Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 推特直播时为什么会出现延迟?_Python_Twitter_Tweepy_Tweets - Fatal编程技术网

Python 推特直播时为什么会出现延迟?

Python 推特直播时为什么会出现延迟?,python,twitter,tweepy,tweets,Python,Twitter,Tweepy,Tweets,我试图通过tweepy流从特定用户那里获取实时推文数据,但是我发现推文发布的准确时间戳和tweepy程序打印文本的时间戳之间有4秒的延迟。这是正常的/预期的还是有一种方法可以提高代码的效率?谢谢 # # # # TWITTER STREAMER # # # # class TwitterStreamer(): """ Class for streaming and processing live tweets. ""&qu

我试图通过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()

这或多或少是真的,是的。延迟取决于许多事情,如网络连接和位置,但通常我希望几秒钟的小延迟。

啊,好吧,很公平。我只是觉得奇怪,每次的延迟正好是4秒,所以我想这可能与我的代码有关。谢谢你认为使用TwitterAPI或Twiython会比tweepy更好吗?不太可能,这更可能是一个网络相关问题,如果是这样的话。我可以通过在靠近Twitter服务器的机器上运行程序来缓解这个问题吗?嘿,matt!我正在做类似的事情,得到了类似的3/4秒延迟。。。您是否设法在最后减少了延迟?