百木园-与人分享,
就是让自己快乐。

Python 进程 Process 与线程 threading 区别 - Python零基础入门教程

目录

  • 一.Python 线程 threading 创建
  • 二.Python 进程 Process 创建
  • 三.Python 进程 Process 和线程 threading 区别
  • 四.Python 进程 Process 并行
  • 五.Python 线程 threading 并发
  • 六.猜你喜欢

一.Python 线程 threading 创建

对于 Python 线程相关的函数本文不再做详细讲解,如果想学习线程 threading 内容请参考:Python 线程创建和参数传递

# !usr/bin/env python
# -*- coding:utf-8 _*-
\"\"\"
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python 进程 Process 与线程 threading 区别.py
@Time:2021/05/07 08:00
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!

\"\"\"

import threading

def study_info(*args,**kwargs):
print(args,kwargs)

def main():

# 信息列表
list_info = [{\"name\":\"python 基础\",\"progress\":\"10%\"},
{\"name\": \"python 面向对象\", \"progress\": \"20%\"},
{\"name\": \"python 爬虫\", \"progress\": \"30%\"},
{\"name\": \"python pyqt5\", \"progress\": \"40%\"},
{\"name\": \"python 数据结构\", \"progress\": \"50%\"},]

# 创建线程
for i in range(5):
p = threading.Thread(target=study_info,args=(i,),kwargs=list_info[i])
# 启动线程
p.start()

if __name__ == \"__main__\":
main()

\'\'\'
输出结果:

(0,) {\'name\': \'python 基础\', \'progress\': \'10%\'}
(1,) {\'name\': \'python 面向对象\', \'progress\': \'20%\'}
(2,) {\'name\': \'python 爬虫\', \'progress\': \'30%\'}
(3,) {\'name\': \'python pyqt5\', \'progress\': \'40%\'}
(4,) {\'name\': \'python 数据结构\', \'progress\': \'50%\'}
\'\'\'

二.Python 进程 Process 创建

对于 Python 进程相关的函数本文不再做详细讲解,如果想学习进程 Process 内容请参考:Python 进程 Process

# !usr/bin/env python
# -*- coding:utf-8 _*-
\"\"\"
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python 进程 Process 与线程 threading 区别.py
@Time:2021/05/07 08:00
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!

\"\"\"

from multiprocessing import Process

def study_info(*args,**kwargs):
print(args,kwargs)

def main():

# 信息列表
list_info = [{\"name\":\"python 基础\",\"progress\":\"10%\"},
{\"name\": \"python 面向对象\", \"progress\": \"20%\"},
{\"name\": \"python 爬虫\", \"progress\": \"30%\"},
{\"name\": \"python pyqt5\", \"progress\": \"40%\"},
{\"name\": \"python 数据结构\", \"progress\": \"50%\"},]

# 创建进程
for i in range(5):
p = Process(target=study_info,args=(i,),kwargs=list_info[i])
# 启动进程
p.start()

if __name__ == \"__main__\":
main()

\'\'\'
输出结果:

(0,) {\'name\': \'python 基础\', \'progress\': \'10%\'}
(1,) {\'name\': \'python 面向对象\', \'progress\': \'20%\'}
(2,) {\'name\': \'python 爬虫\', \'progress\': \'30%\'}
(3,) {\'name\': \'python pyqt5\', \'progress\': \'40%\'}
(4,) {\'name\': \'python 数据结构\', \'progress\': \'50%\'}
\'\'\'

三.Python 进程 Process 和线程 threading 区别

Python 进程 Process 和线程 threading 区别:

1.一个线程只能属于一个进程,而一个进程可以有多个线程,但至少有一个线程(线程是计算机的最小单位);

2.资源分配给进程,同一进程的所有线程共享该进程的所有资源,进程与进程之间资源相互独立,互不影响(类似深拷贝);

3.多进程模式最大的优点就是稳定性高,因为一个子进程崩溃了,不会影响主进程和其他子进程,多进程模式的缺点是在 Windows 下创建进程开销巨大。另外,操作系统能同时运行的进程数也是有限的,在内存和 CPU 的限制下,如果有几千个进程同时运行,操作系统连调度都会成问题(进程的创建比线程的创建更加占用计算机资源);

4.多线程模式致命的缺点就是任何一个线程挂掉都可能直接造成整个进程崩溃,因为所有线程共享进程的内存;

**5.由于 GIL 锁的缘故,Python 中线程实际上是并发运行(即便有多个 CPU** **,线程会在其中一个 CPU** **来回切换,只占用一个 CPU** **资源),而进程才是真正的并行(同时执行多个任务,占用多个 CPU** 资源),下面关于并行和并发做一个简单的了解;

四.Python 进程 Process 并行

并行是指两个或者多个事件在同一时刻发生,Python 中的进程属于并行,能充分利用计算机资源,效率最高,**同时执行多个任务,占用多个 CPU** **资源;**

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BMVew0rZ-1624930849839)(wp-content/uploads/2022/02/http://www.173top.cn/wp-content/uploads/2022/02/1645375436-20220220164356-62126fcc75a85.png “Python 进程 Process 与线程 threading 区别-猿说编程”)]

五.Python 线程 threading 并发

并发是指两个或多个事件在同一时间间隔发生,Python 中的线程属于并发,不管计算机有多少个 CPU ,不管你开了多少个线程,同一时间多个任务会在其中一个 CPU 来回切换,只占用一个 CPU ,效率并不高;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zu34UfQa-1624930849841)(wp-content/uploads/2022/02/http://www.173top.cn/wp-content/uploads/2022/02/1645375436-20220220164356-62126fcc9bd24.png “Python 进程 Process 与线程 threading 区别-猿说编程”)]

关于并行和并发我们留到后面 GIL 锁在详细讲解;

六.猜你喜欢

  • Python 条件推导式
  • Python 列表推导式
  • Python 字典推导式
  • Python 函数声明和调用
  • Python 不定长参数 *argc/**kargcs
  • Python 匿名函数 lambda
  • Python return 逻辑判断表达式
  • Python 字符串/列表/元组/字典之间的相互转换
  • Python 局部变量和全局变量
  • Python type 函数和 isinstance 函数区别
  • Python is 和 == 区别
  • Python 可变数据类型和不可变数据类型
  • Python 浅拷贝和深拷贝
  • Python 文件读写操作
  • Python 异常处理
  • Python 模块 import
  • Python __name__ == ‘__main__’详细解释
  • Python 线程创建和传参
  • Python 线程互斥锁 Lock
  • Python 线程时间 Event
  • Python 线程条件变量 Condition
  • Python 线程定时器 Timer
  • Python 线程信号量 Semaphore
  • Python 线程障碍对象 Barrier
  • Python 线程队列 Queue – FIFO
  • Python 线程队列 LifoQueue – LIFO
  • Python 线程优先队列 PriorityQueue
  • Python 线程池 ThreadPoolExecutor(一)
  • Python 线程池 ThreadPoolExecutor(二)
  • Python 进程 Process 模块
  • Python 进程 Process 与线程 threading 区别
  • Python 进程间通信 Queue / Pipe
  • 未经允许不得转载:猿说编程 » Python 进程 Process 与线程 threading 区别

    本文由博客 - 猿说编程 猿说编程 发布!

    来源:https://www.cnblogs.com/shuopython/p/14953188.html
    图文来源于网络,如有侵权请联系删除。

    未经允许不得转载:百木园 » Python 进程 Process 与线程 threading 区别 - Python零基础入门教程

    相关推荐

    • 暂无文章