并发模块(一):thread、threading和select

本文介绍了Python中的并发编程技术,对比了thread和threading模块的区别,并推荐使用threading模块。此外,文章还提到了由于全局解释器锁(GIL)的存在,多线程在CPU密集型任务上可能无法充分利用多核处理器资源,而IO密集型任务则可以通过多线程获得较好的性能提升。最后简要提及了select模块。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 综述:

*thread模块缺点较多,建议直接使用threading模块。

*Due to the Global Interpreter Lock(GIL), in CPython only one thread can execute Python code at once. If you want your application to make better of use of the computational resources of multi-core machines, you are advised to usemultiprocessing. However, threading is still an appropriate model if you want to runmultiple I/O-bound tasks simultaneously.


二 thread模块:暂不介绍。


三 threading模块:

(1)属性。


(2)注意:

*当子线程不是守护线程时,主线程执行完后会等待子线程结束;否则主线程将退出(子线程好像也终止了),与pthread库不太一样。


四 select模块



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值