您的位置:首页 > 文旅 > 旅游 > 【30天玩转python】多线程与多进程编程

【30天玩转python】多线程与多进程编程

2024/10/5 21:21:35 来源:https://blog.csdn.net/weixin_39372311/article/details/142244195  浏览:    关键词:【30天玩转python】多线程与多进程编程

多线程与多进程编程

在 Python 中,多线程和多进程是实现并发编程的重要手段。多线程适用于 I/O 密集型任务,而多进程则更适合 CPU 密集型任务。通过合理使用多线程和多进程,可以有效提升程序的执行效率和性能。


1. 多线程编程

1.1 线程的概念

线程是操作系统能够进行调度的最小单位。每个线程包含了一个独立的执行路径,多个线程可以在同一个进程内共享内存和资源。Python 中的多线程编程主要使用 threading 模块。

1.2 创建多线程

在 Python 中,可以通过继承 threading.Thread 类来创建新的线程,也可以使用 threading 模块直接创建线程对象。

示例:创建并启动线程

import threading
import timedef print_numbers():for i in range(1, 6):time.sleep(1)print(f"Number: {i}")# 创建线程
t = threading.Thread(target=print_numbers)# 启动线程
t.start()# 等待线程执行完毕
t.join()print("线程执行完毕")

在这个例子中,print_numbers 函数会在新线程中运行,主线程继续等待直到新线程执行完毕后,才打印“线程执行完毕”。

1.3 使用继承创建线程
class MyThread(threading.Thread):def run(self):for i in range(1, 6):time.sleep(1)print(f"MyThread Number: {i}")# 创建并启动线程
t = MyThread()
t.start()
t.join()
1.4 线程锁(Lock)

在多线程环境下,由于多个线程共享内存和资源,可能会出现竞争条件(race condition),从而导致数据不一致或错误。为了解决这个问题,可以使用线程锁(threading.Lock)来同步线程。

示例:使用线程锁

lock = threading.Lock()
counter = 0def increment_counter():global counterfor _ in range(1000000):with lock:counter += 1t1 = threading.Thread(target=increment_counter)
t2 = threading.Thread(target=increment_counter)t1.start()
t2.start()t1.join()
t2.join()print(f"Final counter: {counter}")

在这个例子中,使用 with lock 确保每个线程在修改 counter 时,不会同时被其他线程修改,从而避免竞争条件。


2. 多进程编程

2.1 进程的概念

进程是资源分配的最小单位,每个进程都有自己独立的内存空间。多进程编程可以通过创建多个独立的进程来实现并发。Python 中的多进程编程主要使用 multiprocessing 模块。

2.2 创建多进程

multiprocessing.Process 类提供了与 threading.Thread 类类似的 API,用于创建新进程。

示例:创建并启动进程

import multiprocessing
import timedef print_numbers():for i in range(1, 6):time.sleep(1)print(f"Process Number: {i}")# 创建进程
p = multiprocessing.Process(target=print_numbers)# 启动进程
p.start()# 等待进程执行完毕
p.join()print("进程执行完毕")

与多线程不同的是,每个进程都有独立的内存空间,因此需要通过进程间通信(IPC)机制来共享数据。

2.3 使用继承创建进程
class MyProcess(multiprocessing.Process):def run(self):for i in range(1, 6):time.sleep(1)print(f"MyProcess Number: {i}")# 创建并启动进程
p = MyProcess()
p.start()
p.join()
2.4 进程间通信

由于进程之间不共享内存,因此需要通过管道(Pipe)或队列(Queue)来实现进程间通信。

示例:使用队列进行进程间通信

import multiprocessingdef worker(q):q.put([42, None, 'hello'])if __name__ == "__main__":q = multiprocessing.Queue()p = multiprocessing.Process(target=worker, args=(q,))p.start()print(q.get())  # 从队列中获取数据p.join()

在这个例子中,主进程通过队列与子进程通信,子进程将数据放入队列,主进程从队列中读取数据。


3. 多线程 vs 多进程

特性多线程多进程
内存共享线程间共享内存进程间不共享内存
创建开销线程创建开销较小进程创建开销较大
适用场景I/O 密集型任务(如文件读写、网络)CPU 密集型任务(如复杂计算)
数据隔离线程间数据不隔离,需同步进程间数据完全隔离,需通信
并行性受限于 Python 的 GIL(全局解释器锁)进程可以真正并行执行
故障隔离一个线程崩溃可能影响整个进程进程崩溃不会影响其他进程

4. Global Interpreter Lock (GIL)

Python 中的全局解释器锁(GIL)限制了多个线程同时执行 Python 字节码。尽管可以使用多线程,但在 CPU 密集型任务中,GIL 会导致 Python 程序无法真正并行执行。因此,CPU 密集型任务通常使用多进程来绕过 GIL 的限制。


5. 线程池与进程池

如果需要管理大量线程或进程,使用线程池或进程池会更高效。Python 提供了 concurrent.futures.ThreadPoolExecutorconcurrent.futures.ProcessPoolExecutor 来简化多线程和多进程的管理。

5.1 使用线程池
from concurrent.futures import ThreadPoolExecutordef task(n):return n * 2with ThreadPoolExecutor(max_workers=3) as executor:futures = [executor.submit(task, i) for i in range(5)]for future in futures:print(future.result())
5.2 使用进程池
from concurrent.futures import ProcessPoolExecutordef task(n):return n * 2with ProcessPoolExecutor(max_workers=3) as executor:futures = [executor.submit(task, i) for i in range(5)]for future in futures:print(future.result())

6. 小结

  • 多线程:适用于 I/O 密集型任务,线程间共享内存,但需使用锁同步以避免竞争条件。由于 GIL 的存在,CPU 密集型任务中多线程效果有限。
  • 多进程:适用于 CPU 密集型任务,进程之间数据隔离,真正实现并行执行。适合处理大规模计算任务或需要并行处理的任务。

通过合理选择多线程或多进程,可以显著提高程序的执行效率。在开发过程中,需要根据具体场景选择合适的并发编程模型。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com