Python多进程multiprocessing模块如何实战提升程序并发效率?

2026-05-21 19:342阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计537个文字,预计阅读时间需要3分钟。

Python多进程multiprocessing模块如何实战提升程序并发效率?

在多线程程序中,虽然使用了threading模块,但有时仍需使用multiprocessing模块。以下是对多进程和多线程知识的梳理及代码实践:

多进程(multiprocessing)与多线程(threading)对比:- CPU密集型计算:多进程可以利用多核CPU的优势,实现并行计算;多线程则受限于全局解释器锁(GIL)的限制,效率较低。- I/O密集型计算:多线程能更有效地利用I/O资源,提高效率;多进程则会因进程间通信和切换而消耗更多资源。

代码实践:pythonimport multiprocessingimport threading

def task(): print(Task is running...)

if __name__==__main__: # 单线程 threading.Thread(target=task).start()

# 多线程 for i in range(5): threading.Thread(target=task).start()

# 多进程 pool=multiprocessing.Pool(processes=5) for i in range(5): pool.apply_async(task) pool.close() pool.join()

结果分析:- 单线程:任务按顺序执行。- 多线程:任务并行执行,但受限于GIL。- 多进程:任务并行执行,有效利用多核CPU。

有了多线程threading,为什么还要用多进程multiprocessing

多进程multiprocessing知识梳理(对比多线程threading)

代码实战:单线程、多线程、多进程对比CPU密集计算速度

tmp/06.thread_process_cpu_bound.py

import math
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import time


PRIMES = [112272535095293] * 100



def is_prime(n):
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
sqrt_n = int(math.floor(math.sqrt(n)))
for i in range(3, sqrt_n + 1, 2):
if n % i == 0:
return False
return True


def single_thread():
for number in PRIMES:
is_prime(number)


def multi_thread():
with ThreadPoolExecutor() as pool:
pool.map(is_prime, PRIMES)


def multi_process():
with ProcessPoolExecutor() as pool:
pool.map(is_prime, PRIMES)


if __name__ == '__main__':
start = time.time()
single_thread()
end = time.time()
print("single_thread, cost: ", end - start, "seconds")

start = time.time()
multi_thread()
end = time.time()
print("multi_thread, cost: ", end - start, "seconds")

start = time.time()
multi_process()
end = time.time()
print("multi_process, cost: ", end - start, "seconds")

运行结果:

Python多进程multiprocessing模块如何实战提升程序并发效率?



本文共计537个文字,预计阅读时间需要3分钟。

Python多进程multiprocessing模块如何实战提升程序并发效率?

在多线程程序中,虽然使用了threading模块,但有时仍需使用multiprocessing模块。以下是对多进程和多线程知识的梳理及代码实践:

多进程(multiprocessing)与多线程(threading)对比:- CPU密集型计算:多进程可以利用多核CPU的优势,实现并行计算;多线程则受限于全局解释器锁(GIL)的限制,效率较低。- I/O密集型计算:多线程能更有效地利用I/O资源,提高效率;多进程则会因进程间通信和切换而消耗更多资源。

代码实践:pythonimport multiprocessingimport threading

def task(): print(Task is running...)

if __name__==__main__: # 单线程 threading.Thread(target=task).start()

# 多线程 for i in range(5): threading.Thread(target=task).start()

# 多进程 pool=multiprocessing.Pool(processes=5) for i in range(5): pool.apply_async(task) pool.close() pool.join()

结果分析:- 单线程:任务按顺序执行。- 多线程:任务并行执行,但受限于GIL。- 多进程:任务并行执行,有效利用多核CPU。

有了多线程threading,为什么还要用多进程multiprocessing

多进程multiprocessing知识梳理(对比多线程threading)

代码实战:单线程、多线程、多进程对比CPU密集计算速度

tmp/06.thread_process_cpu_bound.py

import math
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import time


PRIMES = [112272535095293] * 100



def is_prime(n):
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
sqrt_n = int(math.floor(math.sqrt(n)))
for i in range(3, sqrt_n + 1, 2):
if n % i == 0:
return False
return True


def single_thread():
for number in PRIMES:
is_prime(number)


def multi_thread():
with ThreadPoolExecutor() as pool:
pool.map(is_prime, PRIMES)


def multi_process():
with ProcessPoolExecutor() as pool:
pool.map(is_prime, PRIMES)


if __name__ == '__main__':
start = time.time()
single_thread()
end = time.time()
print("single_thread, cost: ", end - start, "seconds")

start = time.time()
multi_thread()
end = time.time()
print("multi_thread, cost: ", end - start, "seconds")

start = time.time()
multi_process()
end = time.time()
print("multi_process, cost: ", end - start, "seconds")

运行结果:

Python多进程multiprocessing模块如何实战提升程序并发效率?