Forums

tweepy streaming API give Error 504 Gateway Time-out

Hello there , i have been facing this issue that i start the streaming from tweepy and getting tweets. i have tested my code on console and on the website ,

  1. on the console it will run for hours
  2. on the website it only runs for 5min

The Error is This :

Error 504 Gateway Time-out

the Code is This :

from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import tweepy
import datetime


##################################################
##################################################
# Autontication Keys from Twitter api --- (DO NOT CHANGE)
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

streaming = []

class StdOutListener(StreamListener):

    def on_status(self, status):
        tweet = status.text.encode('ascii', 'ignore')
        tweet = tweet.replace('\n', '\\n')
        streaming.append({"text": tweet})
        return True

    def on_error(self, status):
        streaming.append(status)
        return False

    def on_limit(self, status):
        streaming.append(status)
        return False

    def on_timeout(self, status):
        streaming.append(status)
        return False

    def on_disconnect(self, notice):
        streaming.append(notice)
        return False


l = StdOutListener()
stream = Stream(auth, l)
stream.filter(track=['basketball'], languages=["en"])

print streaming

The code works for interval under 5min if i force the streaming to stop i can get the tweets but i want it to work for more than 5min where it should but this Error stopping it no idea why , idk if this is from the host or from twitter api.

What is it you're trying to do with the twitter API? The web server wouldn't be the right place for a long-running process. Web server processes are meant to fit into a requests/response model -- they get a request from a browser, they send a response to it, and that's expected to be a very fast cycle, maybe 100ms. The web server assumes that any process that doesn't finish processing a request within a certain time (300s, so 5 minutes) must be stuck, so they get killed automatically.

It really depends what it is you're trying to achieve, but you could look into using a scheduled task as a way to achieve a sort of "always-on" process: https://www.pythonanywhere.com/wiki/LongRunningTasks

This is was what it meant i wanted to test it before i make a scheduled.

so i guess this is the answer. thanks a lot.

:)