Skip to main content
 首页 » 编程设计

python之优雅地停止线程

2024年11月24日7linjiqin

我即将完成我的程序,我想在发布我的第一个版本之前修复这个错误。

这就是我所说的错误:

Exception ignored in: <module 'threading' from '/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py'> 
Traceback (most recent call last): 
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 1281, in _shutdown 
    t.join() 
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 1032, in join 
    self._wait_for_tstate_lock() 
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 1048, in _wait_for_tstate_lock 
    elif lock.acquire(block, timeout): 
KeyboardInterrupt 

我这样启动线程:

def init(self): 
 
    new_post_stream_thread = threading.Thread(target=self.new_post_stream) 
    process_posts = threading.Thread(target=self.process_posts) 
    print('\nStarting threads...') 
    print('- - - - - - - - - -\n') 
    new_post_stream_thread.start() 
    process_posts.start() 

基本上就是这样。我将如何捕获此异常?我试过 try except already 但它什么也没抓到。

这是 MCVE:

import threading 
import time 
 
class MCVE: 
 
    def __init__(self): 
 
        self.list_1 = [] 
 
        self.init() 
 
    def thread_1(self): 
 
        while True: 
            self.list_1.append('whatever') 
            time.sleep(5) 
 
    def thread_2(self): 
 
        while True: 
            for id, entry in enumerate(self.list_1): 
                print('ID {} | entry: {}'.format(id, entry)) 
            time.sleep(10) 
 
    def init(self): 
        thread_1 = threading.Thread(target=self.thread_1) 
        thread_2 = threading.Thread(target=self.thread_2) 
        print('\nStarting threads...') 
        print('- - - - - - - - - -\n') 
        thread_1.start() 
        thread_2.start() 
 
 
def main(): 
 
    example = MCVE() 
 
if __name__ == "__main__": 
    main() 

请您参考如下方法:

您没有提供可重现的示例,但这成功地停止了程序而没有引发未捕获的异常:

def foo(n): 
    import time 
    try: 
        for i in range(n): 
            time.sleep(i) 
    except KeyboardInterrupt: 
        print("Clean exit....") 
        return