2013-04-24 76 views
0

我想用简单的CTR + C停止线程,但数据不停地来,所以我必须关闭窗口并重新启动它。请帮我解决问题。为了看到我在说什么,只是在用户键入您的Twitter用户名和密码,并通过仍然无法停止线程

import threading 
import random 
import time 
import Queue 
import urllib2 
import sys 
import simplejson, pycurl 
import sys, signal 

queue = Queue.Queue() 
keep_running = True 
user = "" # username 
pswd = "" # password 
headers = [ "Content-Type: text/xml; charset: UTF-8; "] 

class MyThread(threading.Thread): 


    def __init__(self, queue): 
     threading.Thread.__init__(self) 
     self.queue = queue 
     self.buffer = "" 
     self.streaming_url = "https://stream.twitter.com/1/statuses/filter.json?track=justinbieber" 

    def start_crawl(self, user, pswd): 
     self.conn = pycurl.Curl() 
     self.conn.setopt(pycurl.USERPWD, "%s:%s" % (user, pswd)) 
     self.conn.setopt(pycurl.URL, self.streaming_url) 
     #self.conn.setopt(pycurl.POST, 1) 
     self.conn.setopt(pycurl.HTTPHEADER, headers) 
     self.conn.setopt(pycurl.READFUNCTION, self.storage) 
     self.conn.setopt(pycurl.VERBOSE, 1) 
     self.conn.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC) 
     self.conn.perform() 


    def storage(self, data): 
     self.buffer += str(data) 
     if isinstance(self.buffer, str): 
      if self.buffer.endswith("\r\n") and self.buffer.strip(): 
       content = json.loads(self.buffer) 
       self.buffer = "" 
       self.queue.put(content) 
       self.queue.task_done() 
       #if "text" in content: 
        #print u"{0[user][name]}: {0[text]}".format(content) 
     else: 
      return data 

    def run(self): 
     while keep_running: 
      self.start_crawl(user,pswd) 
      if signal.signal(signal.SIGINT, signal.SIG_DFL): 
       sys.exit() 
      #line = self.names[random.randint(0,len(self.names)-1)] 
      #queue.put(line) 


class Starter(): 
    def __init__(self): 
     self.queue = queue 
     t = MyThread(self.queue) 
     t.start() 
     self.next() 

    def next(self): 
     while True: 
      time.sleep(0.1) 
      if not self.queue.empty(): 
       line = self.queue.get(timeout=0.2) 
       print line, self.queue.qsize() 
      else: 
       print 'waiting for queue' 

def main(): 
    try: 
     Starter() 
     queue.join() 
    except KeyboardInterrupt, e: 
     print 'Stopping' 
     global keep_running 
     keep_running = False 
     #Join all existing threads to main thread. 
     for thread in threading.enumerate(): 
      if thread is not threading.currentThread(): 
       thread.join() 
     sys.exit(1) 

main() 
+0

我已经被杀死试过了,它工作得很好。只要按住ctrl + c,如果不工作,就不要在输出上打印那么多的数据。 – nacholibre 2013-04-24 20:48:46

回答

2

设置线程作为守护进程,它使你的程序

import threading 
import random 
import time 
import Queue 
import urllib2 
import sys 
import simplejson, pycurl 
import sys, signal 

queue = Queue.Queue() 
keep_running = True 
user = "" # username 
pswd = "" # password 
headers = [ "Content-Type: text/xml; charset: UTF-8; "] 

class MyThread(threading.Thread): 


    def __init__(self, queue): 
     threading.Thread.__init__(self) 
     self.queue = queue 
     self.buffer = "" 
     self.streaming_url = "https://stream.twitter.com/1/statuses/filter.json?track=justinbieber" 

    def start_crawl(self, user, pswd): 
     self.conn = pycurl.Curl() 
     self.conn.setopt(pycurl.USERPWD, "%s:%s" % (user, pswd)) 
     self.conn.setopt(pycurl.URL, self.streaming_url) 
     #self.conn.setopt(pycurl.POST, 1) 
     self.conn.setopt(pycurl.HTTPHEADER, headers) 
     self.conn.setopt(pycurl.READFUNCTION, self.storage) 
     self.conn.setopt(pycurl.VERBOSE, 1) 
     self.conn.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC) 
     self.conn.perform() 


    def storage(self, data): 
     self.buffer += str(data) 
     if isinstance(self.buffer, str): 
      if self.buffer.endswith("\r\n") and self.buffer.strip(): 
       content = json.loads(self.buffer) 
       self.buffer = "" 
       self.queue.put(content) 
       self.queue.task_done() 
       #if "text" in content: 
        #print u"{0[user][name]}: {0[text]}".format(content) 
     else: 
      return data 

    def run(self): 
     while keep_running: 
      self.start_crawl(user,pswd) 
      if signal.signal(signal.SIGINT, signal.SIG_DFL): 
       sys.exit() 
      #line = self.names[random.randint(0,len(self.names)-1)] 
      #queue.put(line) 


class Starter(): 
    def __init__(self): 
     self.queue = queue 
     t = MyThread(self.queue) 
     t.daemon = True 
     t.start() 
     self.next() 

    def next(self): 
     while True: 
      time.sleep(0.1) 
      if not self.queue.empty(): 
       line = self.queue.get(timeout=0.2) 
       print line, self.queue.qsize() 
      else: 
       print 'waiting for queue' 

def main(): 
    try: 
     Starter() 
     queue.join() 
    except KeyboardInterrupt, e: 
     print 'Stopping' 
     raise 

main() 
+0

可以请你给我一些代码示例.. – Vor 2013-04-24 20:40:19

+1

在t.start()之前,写t.daemon = True – spicavigo 2013-04-24 20:43:18

+0

不行,没帮忙 – Vor 2013-04-24 20:44:22