进程、线程之join方法


def join(self,timeout=None)

  """Wait until the thread terminates """

  This blocks the calling thread until the thread whose join() method is called terminates  -- either normally or through an unhandled

      exception or until the optional timeout occurs.

  这将阻止调用进程直到调用join()方法的进程终止--正常 或者 抛出一个非处理的异常 或者 直到 可选 的参数 timeout发生.

  When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions                     thereof). As join() always returns None, you must call isAlive() after join() to decide whether a timeout happened -- if the thread is still alive, the join() call                 timeout.

  当timeout参数存在且不是None时,它应该是一个浮点数指明操作的超时时间(以秒为单位)。 join()方法总是返回None,你必须在调用 join() 方法之后 调用  isAlive()方法 决定超时是否发生--如果线程仍处于活动状态,说明 join()调用超时。

 

  When the timeout argument is not present or None, the operation will block until the thread terminates.

  A thread can be join()ed many times.

  join() raises a RuntimeError if an attempt is made to join the cureent thread as that would cause a deadblock.

  It is also an error to join() a thread  before it has been started and attempts to do so raises the same exception.

  如果尝试 join 当前线程,join()方法会抛出 运行时错误异常 ,因为那样会导致死锁。 在一个线程开始调用start()方法之前,调用join()方法,也会抛出同样的异常。

 

 

import time
from threading import Timer,Thread

def func():
    print(time.strftime("%Y-%m-%d %H:%M:%S"))
    # timer.join() # RuntimeError: cannot join current thread 如果尝试加入当前线程 join()方法,会抛出运行时错误 异常,因为这会导致死锁
    print("执行我啦")

if __name__ == '__main__':
    print(time.strftime("%Y-%m-%d %H:%M:%S"))
    timer = Timer(1,func)  # 当创建线程后,延时1秒后执行
    # timer.join()   # RuntimeError: cannot join thread before it is started  在线程创建启动之前,运行join()方法,抛出 运行时错误 异常
    timer.start()  # 创建线程,并执行线程
    timer.join()
    print(time.strftime("%Y-%m-%d %H:%M:%S"))

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM