zoukankan      html  css  js  c++  java
  • 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。

  • 相关阅读:
    自实现的DNetStopWatch类
    IL Discovery 系列三 《为什么在遍历List<T>对象时同时删除其中项会抛出异常》
    高效的线程安全队列ConcurrentQueue<T>(上)
    .NET中Object.Equals()方法与Object.ReferenceEquals()方法
    麻省理工学院(MIT)的开放课程(OCW)真的不错,其音像资料
    Eclipse快捷键大全
    MyEclipse快捷键大全
    c#单文件上传下载源代码
    Tomcat 配置集锦
    asp.net(C#)多文件上传(源代码)vs2008
  • 原文地址:https://www.cnblogs.com/LoganChen/p/11889081.html
Copyright © 2011-2022 走看看