python thread之join
2011-07-13 14:18
211 查看
join(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.
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 is_alive() after join() to decide whether a timeout happened – if the thread is still alive, the join() call timed out.
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 current thread as that would cause a deadlock. It is also an error to join() a thread before it has been started and attempts to do so raises the same exception.
转自:http://hi.baidu.com/dapengchu/blog/item/6ffae53edfff063671cf6c94.html
Join的作用是众所周知的,阻塞进程直到线程执行完毕。通用的做法是我们启动一批线程,最后join这些线程结束,例如:
for i in range(10):
t = ThreadTest(i)
thread_arr.append(t)
for i in range(10):
thread_arr[i].start()
for i in range(10):
thread_arr[i].join()
此处join的原理就是依次检验线程池中的线程是否结束,没有结束就阻塞直到线程结束,如果结束则跳转执行下一个线程的join函数。
而py的join函数还有一个特殊的功能就是可以设置超时,如下:
Thread.join([timeout]) 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一个参数来设置超时,也就是超过指定时间join就不在阻塞进程。而在实际应用测试的时候发现并不是所有的线程在超时时间内都结束的,而是顺序执行检验是否在time_out时间内超时,例如,超时时间设置成2s,前面一个线程在没有完成的情况下,后面线程执行join会从上一个线程结束时间起再设置2s的超时。
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 is_alive() after join() to decide whether a timeout happened – if the thread is still alive, the join() call timed out.
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 current thread as that would cause a deadlock. It is also an error to join() a thread before it has been started and attempts to do so raises the same exception.
转自:http://hi.baidu.com/dapengchu/blog/item/6ffae53edfff063671cf6c94.html
Join的作用是众所周知的,阻塞进程直到线程执行完毕。通用的做法是我们启动一批线程,最后join这些线程结束,例如:
for i in range(10):
t = ThreadTest(i)
thread_arr.append(t)
for i in range(10):
thread_arr[i].start()
for i in range(10):
thread_arr[i].join()
此处join的原理就是依次检验线程池中的线程是否结束,没有结束就阻塞直到线程结束,如果结束则跳转执行下一个线程的join函数。
而py的join函数还有一个特殊的功能就是可以设置超时,如下:
Thread.join([timeout]) 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一个参数来设置超时,也就是超过指定时间join就不在阻塞进程。而在实际应用测试的时候发现并不是所有的线程在超时时间内都结束的,而是顺序执行检验是否在time_out时间内超时,例如,超时时间设置成2s,前面一个线程在没有完成的情况下,后面线程执行join会从上一个线程结束时间起再设置2s的超时。
相关文章推荐
- python中thread的setDaemon、join的用法
- python中thread的setDaemon、join的用法
- Python thread join
- python中thread的setDaemon、join的用法
- python中thread的setDaemon、join的用法
- python中thread的setDaemon、join的用法
- python中thread的setDaemon、join的用法
- python thread的join方法解释
- Python的Threading模块的Thread对象的join方法
- Python 多线程 thread join() 的作用
- python中thread的setDaemon、join的用法
- Thread.join()分析方法
- Python - Thread 模块使用笔记
- How to exit the entire application from a Python thread?
- [python] 专题八.多线程编程之thread和threading
- python thread wait until specific thread complete
- 详解Python中的join()函数的用法
- python中主线程等待子线程完成的实现(join())
- Python多线程thread与threading实现
- python线程池threadpool实现篇