• Python线程join和setDaemon


    看一下线程的setDaemon()方法

    import time
    import threading
    import ctypes
    import inspect
    
    def sayHello():
        for i in range(10):
            print("hello")
            time.sleep(1)
    
    def _async_raise(tid, exctype):
        """raises the exception, performs cleanup if needed"""
        tid = ctypes.c_long(tid)
        if not inspect.isclass(exctype):
            exctype = type(exctype)
        res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
        if res == 0:
            raise ValueError("invalid thread id")
        elif res != 1:
            # """if it returns a number greater than one, you're in trouble,
            # and you should call it again with exc=NULL to revert the effect"""
            ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
            raise SystemError("PyThreadState_SetAsyncExc failed")
    
    def stop_thread(thread):
        _async_raise(thread.ident, SystemExit)
    
    
    
    if __name__ == '__main__':
        # sayHello()
        t = threading.Thread(target=sayHello, args=())
        t.setDaemon(True) # 主线程结束后停止子线程
        t.start()
        for i in range(3):
            print(t.is_alive())
            time.sleep(1)

    上面的输出是:

    hello
    True
    True
    hello
    hello
    True
    hello

    我们修改一下代码:

    import time
    import threading
    import ctypes
    import inspect
    
    def sayHello():
        for i in range(10):
            print("hello")
            time.sleep(1)
    
    def _async_raise(tid, exctype):
        """raises the exception, performs cleanup if needed"""
        tid = ctypes.c_long(tid)
        if not inspect.isclass(exctype):
            exctype = type(exctype)
        res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
        if res == 0:
            raise ValueError("invalid thread id")
        elif res != 1:
            # """if it returns a number greater than one, you're in trouble,
            # and you should call it again with exc=NULL to revert the effect"""
            ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
            raise SystemError("PyThreadState_SetAsyncExc failed")
    
    def stop_thread(thread):
        _async_raise(thread.ident, SystemExit)
    
    
    
    if __name__ == '__main__':
        # sayHello()
        t = threading.Thread(target=sayHello, args=())
        t.setDaemon(False) # 主线程结束后不停止子线程
        t.start()
        for i in range(3):
            print(t.is_alive())
            time.sleep(1)

    程序的输出是:

    hello
    True
    True
    hello
    hello
    True
    hello
    hello
    hello
    hello
    hello
    hello
    hello

    可见,setDaemon()方法就是决定在主线程结束后是否结束子线程,如果为True时,会结束子线程,为False时,不会结束子线程。

    我们再来看join()方法:

    直接看代码

    import time
    import threading
    import ctypes
    import inspect
    
    def sayHello():
        for i in range(10):
            print("hello")
            time.sleep(1)
    
    def _async_raise(tid, exctype):
        """raises the exception, performs cleanup if needed"""
        tid = ctypes.c_long(tid)
        if not inspect.isclass(exctype):
            exctype = type(exctype)
        res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
        if res == 0:
            raise ValueError("invalid thread id")
        elif res != 1:
            # """if it returns a number greater than one, you're in trouble,
            # and you should call it again with exc=NULL to revert the effect"""
            ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
            raise SystemError("PyThreadState_SetAsyncExc failed")
    
    def stop_thread(thread):
        _async_raise(thread.ident, SystemExit)
    
    
    
    if __name__ == '__main__':
        # sayHello()
        t = threading.Thread(target=sayHello, args=())
        t.setDaemon(False) # 主线程结束后不停止子线程
        t.start()
        for i in range(3):
            print(t.is_alive())
            time.sleep(1)
        # t.join()
        print("over")

    输出结果为:

    hello
    True
    True
    hello
    True
    hello
    hello
    over
    hello
    hello
    hello
    hello
    hello
    hello

    可以看到主线程结束时,打印出over,之后子线程还在继续打印hello

    修改代码:

    import time
    import threading
    import ctypes
    import inspect
    
    def sayHello():
        for i in range(10):
            print("hello")
            time.sleep(1)
    
    def _async_raise(tid, exctype):
        """raises the exception, performs cleanup if needed"""
        tid = ctypes.c_long(tid)
        if not inspect.isclass(exctype):
            exctype = type(exctype)
        res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
        if res == 0:
            raise ValueError("invalid thread id")
        elif res != 1:
            # """if it returns a number greater than one, you're in trouble,
            # and you should call it again with exc=NULL to revert the effect"""
            ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
            raise SystemError("PyThreadState_SetAsyncExc failed")
    
    def stop_thread(thread):
        _async_raise(thread.ident, SystemExit)
    
    
    
    if __name__ == '__main__':
        # sayHello()
        t = threading.Thread(target=sayHello, args=())
        t.setDaemon(False) # 主线程结束后不停止子线程
        t.start()
        for i in range(3):
            print(t.is_alive())
            time.sleep(1)
        t.join()
        print("over")

    输出结果为:

    hello
    True
    hello
    True
    True
    hello
    hello
    hello
    hello
    hello
    hello
    hello
    hello
    over

    可以看到设置t.join()方法之后,主线程要等待t这个线程结束之后,才能继续,也就是等hello打印完之后才打印over。

  • 相关阅读:
    #419 Div2 Problem B Karen and Coffee (统计区间重叠部分 && 前缀和)
    #418 Div2 Problem B An express train to reveries (构造 || 全排列序列特性)
    FZU 2203 单纵大法好 (二分 && 贪心)
    51Nod 1413 权势二进制 (思维)
    51Nod 1433 0和5 (数论 && 被9整除数的特点)
    POJ 2492 A Bug's Life (带权并查集 && 向量偏移)
    POJ 1456 Supermarket (贪心 && 并查集优化)
    POJ 1182 食物链 (带权并查集 && 向量偏移)
    #417 Div2 Problem B Sagheer, the Hausmeister (DFS && 枚举)
    #417 Div2 Problem C Sagheer and Nubian Market (二分 && std::accumulate)
  • 原文地址:https://www.cnblogs.com/LoganChen/p/11889081.html
Copyright © 2020-2023  润新知