Python(进程线程)
2017-07-19 08:33
357 查看
一 理论基础:
多线程指的是,在一个进程中开启多个线程,简单的讲:如果多个任务共用一块地址空间,那么必须在一个进程内开启多个线程。详细的讲分为4点: 1. 多线程共享一个进程的地址空间 2. 线程比进程更轻量级,线程比进程更容易创建可撤销,在许多操作系统中,创建一个线程比创建一个进程要快10-100倍,在有大量线程需要动态和快速修改时,这一特性很有用 3. 若多个线程都是cpu密集型的,那么并不能获得性能上的增强,但是如果存在大量的计算和大量的I/O处理,拥有多个线程允许这些活动彼此重叠运行,从而会加快程序执行的速度。 4. 在多cpu系统中,为了最大限度的利用多核,可以开启多个线程(比开进程开销要小的多)View Code
3.3 多线程的应用举例
开启一个字处理软件进程,该进程肯定需要办不止一件事情,比如监听键盘输入,处理文字,定时自动将文字保存到硬盘,这三个任务操作的都是同一块数据,因而不能用多进程。只能在一个进程里并发地开启三个线程,如果是单线程,那就只能是,键盘输入时,不能处理文字和自动保存,自动保存时又不能输入和处理文字。
3.4 线程与进程的区别
- Threads share the address space of the process that created it; processes have their own address space.
- Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.
- Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
- New threads are easily created; new processes require duplication of the parent process.
- Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.
- Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process; changes to the parent process does not affect child processes.
相关文章推荐
- Python 中的线程与进程(四)
- python---进程与线程(四)---process类,进程间通信,进程池,协程
- Python笔记之进程和线程
- Python进程与线程及GIL(全局解释器锁)
- 进程与线程---Python_Daily
- python进程与线程小结实例分析
- Python_进程、线程及协程
- python 之线程、进程、协程
- 进程、线程、协程总结(Python)
- Python学习第二十天——线程、进程与赛车游戏
- 系统进程与线程关系以及使用Python获取的方法
- python线程、进程与协程
- python进程与线程混合使用例子
- 多线程-threading和进程VS线程(python 版)
- python进阶:Python进程、线程、队列、生产者/消费者模式、协程
- Python之进程、线程、协程
- Python中单线程、多线程和多进程的效率对比实验
- python-3-进程和线程
- 廖雪峰Python教程1轮还没学明白的(5) -- 进程和线程 -- 多进程
- python学习进程和线程中碰到的问题以及问题解惑