Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

python - Is it possible to stop twython streaming at certain point of time?

I have the codes for twython streaming and it is working.

def read_cred(file): in_handle = open(file,'r')

cred = {}
for ln in in_handle:
    data = ln.strip('
').split('=')
    if len(data) > 1:
        key = data[0].strip(' ').lower()
        value = data[1].strip(' ')
        cred[key] = value
    else:
        print "error in parsing credentials file"
return cred

cred = read_cred(sys.argv[1])

class MyStreamer(TwythonStreamer): def on_success(self, data): act(data)

def on_error(self, status_code, data):
    print status_code, data

stream = MyStreamer(cred['consumer_key'], cred['consumer_secret'], cred['access_token_key'], cred['access_token_secret'])

keywords = sys.argv[2]

stream.statuses.filter(track=keywords)

However, I want to create a UI in django framework which consist of a 'start' and a 'stop' button. What should I do to stop the twython streaming when I clicked on the button 'stop' ? Can give me some simple examples pls?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As long as the stop button is bound to the rest of the app (I haven't used Django, so I can't provide a simple example) then calling sys.exit() should do the job.

Django might have some other method for terminating a process built into it or some specific examples in its documentation. You should check that to confirm this answer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...