Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to properly close a tweepy stream

I'm trying to figure out how to properly close an asynchronous tweepy stream.

The tweepy streaming module can be found here.

I start the stream like this:

stream = Stream(auth, listener)
stream.filter(track=['keyword'], async=True)

When closing the application, I try to close the stream as simple as:

stream.disconnect()

This method seems to work as intended but it seems to have one problem: the stream thread is still in the middle of the loop (waiting/handling tweets) and is not killed until the next loop, so when the stream receives a tweet even after the app has closed, it still tries to call the listener object (this can be seen with a simple print syntax on the listener object). I'm not sure if this is a bad thing or if it can simply be ignored.

I have 2 questions:

  1. Is this the best way to close the stream or should I take a different approach?
  2. Shouldn't the async thread be created as a daemon thread?
like image 642
Wilco Avatar asked Dec 31 '12 15:12

Wilco


People also ask

Is Tweepy a REST api?

The streaming api is quite different from the REST api because the REST api is used to pull data from twitter but the streaming api pushes messages to a persistent session. This allows the streaming api to download more data in real time than could be done using the REST API. In Tweepy, an instance of tweepy.


2 Answers

I had the same problem. I fixed it with restarting the script. Tweepy Stream doesn't stop until the next incoming tweet.

Example:

import sys
import os

python=sys.executable

time.sleep(10)

print "restart"
os.execl(python,python,*sys.argv)

I didn't find another solution.

like image 155
Burkay Avatar answered Oct 18 '22 12:10

Burkay


I am not positive that it applies to your situation, but in general you can have applicable entities clean up after themselves by putting them in a with block:

with stream = Stream(auth, listener):
    stream.filter(track=['keyword'], async=True)
    # ...
# Outside the with-block; stream is automatically disposed of.

What "disposed of" actually means, it that the entities __exit__ function is called.
Presumably tweepy will have overridden that to Do The Right Thing.
As @VooDooNOFX suggests, you can check the source to be sure.

like image 2
Edward Avatar answered Oct 18 '22 11:10

Edward