2014-11-24 14 views
2

嘗試一個 「停止的」 線程https://stackoverflow.com/a/325528/1619432像這樣:Python的線程self._stop() '事件' 對象不是可調用

import sys 
import threading 
import time 
import logging 

class StoppableThread(threading.Thread): 
    """Thread class with a stop() method. The thread itself has to check 
    regularly for the stopped() condition.""" 

    def __init__(self): 
     print("base init", file=sys.stderr) 
     super(StoppableThread, self).__init__() 
     self._stop = threading.Event() 

    def stop(self): 
     print("base stop()", file=sys.stderr) 
     self._stop.set() 

    def stopped(self): 
     return self._stop.is_set() 


class datalogger(StoppableThread): 
    """ 
    """ 

    import time 

    def __init__(self, outfile): 
     """ 
     """ 
     StoppableThread.__init__(self) 
     self.outfile = outfile 
     print("thread init", file=sys.stderr) 

    def run(self): 
     """ 
     """ 
     print("thread running", file=sys.stderr) 
     while not self.stopped(): 
     print(self.outfile , file=sys.stderr) 
     time.sleep(0.33) 
     print("thread ending", file=sys.stderr) 


test = datalogger("test.txt") 
test.start() 
time.sleep(3) 
logging.debug("stopping thread") 
test.stop() 
logging.debug("waiting for thread to finish") 
test.join() 

給出了錯誤以下的輸出:

> demo.py 
base init 
thread init 
thread running 
test.txt 
test.txt 
test.txt 
test.txt 
test.txt 
test.txt 
test.txt 
test.txt 
test.txt 
test.txt 
base stop() 
thread ending 
Traceback (most recent call last): 
    File "demo.py", line 54, in <module> 
    test.join() 
    File "C:\Python34\lib\threading.py", line 1061, in join 
    self._wait_for_tstate_lock() 
    File "C:\Python34\lib\threading.py", line 1079, in _wait_for_tstate_lock 
    self._stop() 
TypeError: 'Event' object is not callable 

可能有人請解釋我做錯了什麼?

文檔:https://docs.python.org/3.4/library/threading.html#event-objects

回答

5

將溶液在上文提及的答案的評論中提及:self._stop已經由Threading.thread。此修改後的代碼有效:

import sys 
import threading 
import time 
import logging 

class StoppableThread(threading.Thread): 
    """Thread class with a stop() method. The thread itself has to check 
    regularly for the stopped() condition.""" 

    def __init__(self): 
     print("base init", file=sys.stderr) 
     super(StoppableThread, self).__init__() 
     self._stopper = threading.Event() 

    def stopit(self): 
     print("base stop()", file=sys.stderr) 
     self._stopper.set() 

    def stopped(self): 
     return self._stopper.is_set() 


class datalogger(StoppableThread): 
    """ 
    """ 

    import time 

    def __init__(self, outfile): 
     """ 
     """ 
     StoppableThread.__init__(self) 
     self.outfile = outfile 
     print("thread init", file=sys.stderr) 

    def run(self): 
     """ 
     """ 
     print("thread running", file=sys.stderr) 
     while not self.stopped(): 
     print(self.outfile , file=sys.stderr) 
     time.sleep(0.33) 
     print("thread ending", file=sys.stderr) 


test = datalogger("test.txt") 
test.start() 
time.sleep(3) 
logging.debug("stopping thread") 
test.stopit() 
logging.debug("waiting for thread to finish") 
test.join() 
相關問題