在 Python 中使用 多线程 Multithreading, 多进程 Multiprocessing

简介: 线程 Thread / 进程 Process进程一个正在运行的程序进程间内存不共享,通过进程间通信等传递信息线程被包含在进程之中,独立执行相同程序运算调度的最小单位,宏观并行,微观分时切换串行共享同一份全局内存区域创建线程比创建进程通常要快10倍甚至更多线程/进程 池一种管理工具(方法/思想)尽可能减少创建和销毁线程的次数,从而减少时间和资源消耗池化思想复用资源,减少创建和释放的资源消耗对象池、连

线程 Thread / 进程 Process

进程

  • 一个正在运行的程序
  • 进程间内存不共享,通过进程间通信等传递信息

线程

  • 被包含在进程之中,独立执行相同程序
  • 运算调度的最小单位, 宏观并行,微观分时切换串行
  • 共享同一份全局内存区域
  • 创建线程比创建进程通常要快10倍甚至更多

线程/进程 池

  • 一种管理工具 ( 方法 / 思想 )
  • 尽可能减少创建和销毁线程的次数,从而减少时间和资源消耗

池化思想

  • 复用资源,减少创建和释放的资源消耗
  • 对象池、连接池、内存池、线程池

Python 中 线程 Thread / 进程 Process 的使用

最常用的两种方式

ThreadPoolExecutor

ProcessPoolExecutor

使用“池”完成一组同类型任务

executor.map 会保持顺序

multiprocessing.Process

新建进程启动任意任务

例子:

ThreadPoolExecutor

from concurrent.futures import ThreadPoolExecutor

def main():
    with ThreadPoolExecutor() as executor:
        executor.map(download, links, timeout=30)

ProcessPoolExecutor

from concurrent.futures import ProcessPoolExecutor

def main():
    with ProcessPoolExecutor as executor:
        executor.map(is_prime, PRIMES)

具体 API 参考:concurrent.futures — Launching parallel tasks — Python 3.9.4 documentation

Process 

from multiprocessing import Process

def f(name):
    print('hello', name)

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

具体 API 参考:multiprocessing — Process-based parallelism — Python 3.9.4 documentation

Multithreading vs Multiprocessing 

IO bound

multithreading (less overhead), multiprocessing 

CPU bound

multiprocessing

multiple machines

RQ

对于 IO 密集型的任务,使用线程和进程均可,使用线程可以减少资源消耗

对于 CPU 密集型的任务,使用进程

优化

使用全局变量

或 multiprocessing.Queue

Don't pickle the input, This will save a lot of communication overhead, especially if the output is small compared to the input

使用 chunk

ProcessPoolExecutor.map(chunksize =x)

对于大量小计算量任务,使用 chunk 充分利用 Process 资源,减少 overhead

常见问题

死锁

the function needs to be defined at the top-level, nested functions won't be importable by the child and already trying to pickle them raises an exception

Reference

线程池源码浅析 (Java)

线程概念,Java ThreadPoolExecutor

Don't pickle the input (stackoverflow)

 

https://docs.python.org/3/library/multiprocessing.html#module-multiprocessing

官网文档

https://docs.python.org/3/library/threading.html

官网文档

Python Multithreading and Multiprocessing Tutorial

操作系统导论

 

目录
相关文章
|
9天前
|
存储 调度 C++
【操作系统】进程与线程的区别及总结(非常非常重要,面试必考题,其它文章可以不看,但这篇文章最后的总结你必须要看,满满的全是干货......)
【操作系统】进程与线程的区别及总结(非常非常重要,面试必考题,其它文章可以不看,但这篇文章最后的总结你必须要看,满满的全是干货......)
37 1
|
4天前
|
开发框架 并行计算 安全
Python的GIL限制了CPython在多核下的并行计算,但通过替代解释器(如Jython, IronPython, PyPy)和多进程、异步IO可规避
【6月更文挑战第26天】Python的GIL限制了CPython在多核下的并行计算,但通过替代解释器(如Jython, IronPython, PyPy)和多进程、异步IO可规避。Numba、Cython等工具编译优化代码,未来社区可能探索更高级的并发解决方案。尽管GIL仍存在,现有策略已能有效提升并发性能。
12 3
|
3天前
|
数据采集 Java Unix
10-多线程、多进程和线程池编程(2)
10-多线程、多进程和线程池编程
|
3天前
|
安全 Java 调度
10-多线程、多进程和线程池编程(1)
10-多线程、多进程和线程池编程
|
5天前
|
Python
Python多进程编程详细剖析
Python多进程编程详细剖析
14 3
|
5天前
|
算法 API 调度
|
1天前
|
数据采集 XML 存储
【Python实战】Python多线程批量采集图片
【Python实战】Python多线程批量采集图片
|
2天前
|
开发框架 安全 .NET
技术好文共享:进程和线程的区别
技术好文共享:进程和线程的区别
|
2天前
|
开发框架 安全 .NET
程序技术好文:进程和线程的区别
程序技术好文:进程和线程的区别
|
3天前
|
调度 Python
Python多线程学习优质方法分享
Python多线程学习优质方法分享