Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twitter Stream re-connect best practices #554

Closed
dubielt1 opened this issue Mar 30, 2018 · 2 comments
Closed

Twitter Stream re-connect best practices #554

dubielt1 opened this issue Mar 30, 2018 · 2 comments

Comments

@dubielt1
Copy link

I have a project that will consume a long-running twitter stream, and the documentation doesn't mention what will happen when a disconnect occurs. Looking at the code, a requests exception might get caught, at which point python-twitter would raise its own exception?

I want to be sure that I take the best approach for guaranteeing a re-connect after being disconnected.

I apologize if I overlooked anything in the documentation, or if this behaviour is inherent to requests in some way.

@jeremylow
Copy link
Collaborator

No apologies necessary! I didn't know what would happen until you asked.

You can set the requests.Session timeout parameter with something like:

api = twitter.Api(
	CONSUMER_KEY,
	CONSUMER_SECRET,
	ACCESS_TOKEN,
	ACCESS_SECRET,
	tweet_mode="extended",
	timeout=3.0  # three second timeout
)

at which point, if something goes wrong (your connection goes down) you'll get a ConnectionError, which you could catch. If your connection comes back up before you disconnect with a ConnectionError, you might see some other errors. In testing, I got the following:

IncompleteRead,
ChunkedEncodingError,
ProtocolError

etc. I'm not sure that covers all of them, so you might want to implement some logging function and check periodically.

If you don't set a timeout when instantiating the api, then the stream will just hang indefinitely.

For other things, like Twitter disconnecting you, you'll get a TwitterError. They provide pretty detailed documentation on this here: https://developer.twitter.com/en/docs/tutorials/consuming-streaming-data

Hope that helps!

@dubielt1
Copy link
Author

dubielt1 commented Apr 2, 2018

Exactly what I was looking for, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants