Python3 threading.Thread 的jion()用法实例

Python
# -*- coding: utf-8 -*- import time __author__ = 'songhao' import threading def work(): # time.sleep(tt) print("work 开始") for x in range(10): time.sleep(0.1) # print(threading.current_thread()) print("work 结束") def man(): add_work = <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/threading-thread" title="View all posts in threading.Thread" target="_blank">threading.Thread</a></span>(target=work) add_work.start() print("all done") if __name__ == '__main__': man()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# -*- coding: utf-8 -*-
import time
 
__author__ = 'songhao'
import threading
 
 
def work ( ) :
     # time.sleep(tt)
     print ( "work 开始" )
     for x in range ( 10 ) :
         time . sleep ( 0.1 )
     # print(threading.current_thread())
     print ( "work 结束" )
 
 
def man ( ) :
     add_work = threading . Thread ( target = work )
     add_work . start ( )
 
     print ( "all done" )
 
 
if __name__ == '__main__' :
     man ( )

如果不加 jion 输出的结果是什么呢?

 

/usr/local/bin/python3 "/Users/songhao/Desktop/Python3 入门和进阶/Python file/d3/c8.py"
work 开始
all done
work 结束

加上 jion后呢?

Python
# -*- coding: utf-8 -*- import time __author__ = 'songhao' import threading def work(): # time.sleep(tt) print("work 开始") for x in range(10): time.sleep(0.1) # print(threading.current_thread()) print("work 结束") def man(): add_work = <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/threading-thread" title="View all posts in threading.Thread" target="_blank">threading.Thread</a></span>(target=work) add_work.start() add_work.join() print("all done") if __name__ == '__main__': man()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# -*- coding: utf-8 -*-
import time
 
__author__ = 'songhao'
import threading
 
 
def work ( ) :
     # time.sleep(tt)
     print ( "work 开始" )
     for x in range ( 10 ) :
         time . sleep ( 0.1 )
     # print(threading.current_thread())
     print ( "work 结束" )
 
 
def man ( ) :
     add_work = threading . Thread ( target = work )
     add_work . start ( )
     add_work . join ( )
 
     print ( "all done" )
 
 
if __name__ == '__main__' :
     man ( )

结果是:

join(timeout) 在join()位置等待另一线程结束后再继续运行join()后的操作,timeout是可选项,表示最大等待时间




  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
### Python `concurrent.futures` 线程池与 `threading.Thread` 的优劣势分析 #### 1. **易用性和抽象层次** `concurrent.futures.ThreadPoolExecutor` 提供了一个更高层的接口来管理线程池,简化了多线程编程中的复杂性。它通过内置的提交机制和结果获取方式减少了手动管理和同步的需求[^1]。相比之下,`threading.Thread` 需要开发者自行处理线程创建、启动以及资源释放等问题。 ```python from concurrent.futures import ThreadPoolExecutor import threading def task(x): return x * x # 使用ThreadPoolExecutor with ThreadPoolExecutor(max_workers=5) as executor: results = list(executor.map(task, range(10))) print(results) # 使用threading.Thread threads = [] results = [] for i in range(10): t = threading.Thread(target=lambda x: results.append(x*x), args=(i,)) threads.append(t) t.start() for t in threads: t.join() print(results) ``` 上述代码展示了两种实现方式的不同之处,其中 `ThreadPoolExecutor` 更加简洁明了[^2]。 --- #### 2. **性能开销** 尽管两者都可以用来执行并发任务,但在频繁创建销毁线程的情况下,`ThreadPoolExecutor` 显著降低了每次创建新线程带来的额外开销。这是因为线程池会重用已有的工作线程而不是每次都重新实例化一个新的线程对象。而 `threading.Thread` 则可能因为频繁的操作而导致较高的内存消耗和上下文切换成本。 --- #### 3. **适用场景** 对于 IO 密集型的任务(如网络请求或文件读写),无论是 `ThreadPoolExecutor` 还是单独使用的 `threading.Thread` 都能很好地发挥作用,但由于前者提供了更高效的线程复用能力,在大规模并行操作下表现更好。然而需要注意的是,由于 Python 中存在全局解释器锁 (GIL),即使使用多线程也无法真正提升 CPU 计算密集型任务的速度——此时更适合采用 `ProcessPoolExecutor` 或其他替代方案。 --- #### 4. **错误处理和支持功能** 当涉及到异常捕获或者超时控制等功能时,`ThreadPoolExecutor.submit()` 方法返回的一个 Future 对象可以方便地跟踪任务状态及其完成情况,并允许设置回调函数以便进一步处理结果或失败情形。而在原始 `Thread` 类中,则缺乏这种高级别的支持特性,必须依赖外部手段去实现类似的逻辑。 --- 综上所述,虽然二者都能满足基本需求,但从开发效率、维护便利度等方面考虑,推荐优先选用 `concurrent.futures.ThreadPoolExecutor` 来构建基于线程的应用程序结构。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值