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

A way to stop the stream #31

Closed
filipjohansson opened this issue Oct 2, 2014 · 9 comments
Closed

A way to stop the stream #31

filipjohansson opened this issue Oct 2, 2014 · 9 comments
Labels

Comments

@filipjohansson
Copy link

It would be really nice to have a function to stop the stream when running r = api.request('statuses/filter', {'track': TRACK_TERM}).
Something like r.stop().

@geduldig
Copy link
Owner

geduldig commented Oct 2, 2014

If you use r's iterator I don't see the necessity of a stop function. Maybe you have something else in mind. Can you show me a code example of how you would use r.stop() if there were such a method?

@geduldig
Copy link
Owner

geduldig commented Oct 4, 2014

r = api.request('statuses/filter', {'track':TRACK_TERM})
for item in r.get_iterator():
    if stop(item):
        break
    process_tweet(item)

def stop(item):
    if 'code' in item and item['code'] is 88:
        print "Rate limit exceeded."
        return True
    else:
        return False

@filipjohansson
Copy link
Author

The reason I want to stop the api.request is because I want to be able to change the search term on the fly, maybe I'm trying to solve that problem the wrong way?

@geduldig geduldig reopened this Oct 6, 2014
@geduldig
Copy link
Owner

geduldig commented Oct 6, 2014

It really depends on what you want to do. For example, you could get a tweet and change the request and get another tweet this way:

r = api.request('statuses/filter', {'track':TRACK_TERM1})
tweet = next(r.get_iterator())
print tweet['text']
r = api.request('statuses/filter', {'track':TRACK_TERM2})
tweet = next(r.get_iterator())
print tweet['text']

You can see from this example, which uses next, and the previous example, which uses a for loop, there is no starting and stopping of the stream. The request method returns an object which represents the stream you requested. The iterator lets you grab one item from that stream at a time. You can grab as many items as you like, and you can stop grabbing items whenever you like.

I hope that clarifies something.

@filipjohansson
Copy link
Author

If I understand your example it allows me to search for many things at the same time?
What I would like to do is to stop tracking the old one and start tracking the new one. Hard coding the amount of times I can change the search term is not an option either. Best would be if I could just replace the old search term with the new one.

@geduldig
Copy link
Owner

geduldig commented Oct 7, 2014

I think you are misunderstanding. The last example does not do a simultaneous search. It does exactly what you said:

- tracks one term
- replaces the the term with a new one
- tracks a second term

@filipjohansson
Copy link
Author

Yes, I am pretty sure there is something here I do not understand.

My code first looks like this:

r = api.request('statuses/filter', {'track': track_term})
for item in r:
    print(item['text'] if 'text' in item else item)

That works fine. But later in the script I change the value of track_term and replace r. Like this:

track_term = 'pizza'
r = api.request('statuses/filter', {'track': track_term})

The old for loop still outputs the results from the first track_term.

@geduldig
Copy link
Owner

geduldig commented Oct 8, 2014

That sounds right, because the for loop never ends. And, as you said in your first post, you need a way to stop the stream. Maybe what you do not realize is that even if you could call r.stop() it would not be effective for you. You probably would call it just before you wanted to set the new search term; however, your program would still be in the for loop and never reach r.stop(). This is why there is no point in having a stop method.

What I suggest is this. First, ask yourself what is the condition you want the for loop to stop. You have not specified that in this thread. Second, use that condition in the stop function example I provided in my second post.

@filipjohansson
Copy link
Author

It feels like I am looking at this in the wrong way. What you say makes sense, but since I do not have that much experience with Python I am having a hard time implementing it.
I will close this thread now but I am more than happy with all the help you have given me. Thank you very much and keep up the good work!

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

No branches or pull requests

2 participants