跨境互联网 跨境互联网
首页
  • AI 工具

    • 绘图提示词工具 (opens new window)
    • ChatGPT 指令 (opens new window)
  • ChatGPT

    • ChatGP T介绍
    • ChatGPT API 中文开发手册
    • ChatGPT 中文调教指南
    • ChatGPT 开源项目
  • Midjourney

    • Midjourney 文档
  • Stable Diffusion

    • Stable Diffusion 文档
  • 其他

    • AIGC 热门文章
    • 账号合租 (opens new window)
    • 有趣的网站
  • Vue

    • Vue3前置
  • JAVA基础

    • Stream
    • Git
    • Maven
    • 常用第三方类库
    • 性能调优工具
    • UML系统建模
    • 领域驱动设计
    • 敏捷开发
    • Java 测试
    • 代码规范及工具
    • Groovy 编程
  • 并发编程&多线程

    • 并发编程
    • 高性能队列 Disruptor
    • 多线程并发在电商系统下的应用
  • 其他

    • 面试题
  • 消息中间中间件

    • Kafka
    • RabbitMQ
    • RocketMQ
  • 任务调度

    • Quartz
    • XXL-Job
    • Elastic-Job
  • 源码解析

    • Mybatis 高级使用
    • Mybatis 源码剖析
    • Mybatis-Plus
    • Spring Data JPA
    • Spring 高级使用
    • Spring 源码剖析
    • SpringBoot 高级使用
    • SpringBoot 源码剖析
    • Jdk 解析
    • Tomcat 架构设计&源码剖析
    • Tomcat Web应用服务器
    • Zookeeper 高级
    • Netty
  • 微服务框架

    • 分布式原理
    • 分布式集群架构场景化解决方案
    • Dubbo 高级使用
    • Dubbo 核心源码剖析
    • Spring Cloud Gateway
    • Nacos 实战应用
    • Sentinel 实战应用
    • Seata 分布式事务
  • 数据结构和算法的深入应用
  • 存储

    • 图和Neo4j
    • MongoDB
    • TiDB
    • MySQL 优化
    • MySQL 平滑扩容实战
    • MySQL 海量数据存储与优化
    • Elasticsearch
  • 缓存

    • Redis
    • Aerospike
    • Guava Cache
    • Tair
  • 文件存储

    • 阿里云 OSS 云存储
    • FastDF 文件存储
  • 基础

    • Linux 使用
    • Nginx 使用与配置
    • OpenResty 使用
    • LVS+Keepalived 高可用部署
    • Jekins
  • 容器技术

    • Docker
    • K8S
    • K8S
  • 01.全链路(APM)
  • 02.电商终极搜索解决方案
  • 03.电商亿级数据库设计
  • 04.大屏实时计算
  • 05.分库分表的深入实战
  • 06.多维系统下单点登录
  • 07.多服务之间分布式事务
  • 08.业务幂等性技术架构体系
  • 09.高并发下的12306优化
  • 10.每秒100W请求的秒杀架构体系
  • 11.集中化日志管理平台的应用
  • 12.数据中台配置中心
  • 13.每天千万级订单的生成背后痛点及技术突破
  • 14.红包雨的架构设计及源码实现
  • 人工智能

    • Python 笔记
    • Python 工具库
    • 人工智能(AI) 笔记
    • 人工智能(AI) 项目笔记
  • 大数据

    • Flink流处理框架
  • 加密区

    • 机器学习(ML) (opens new window)
    • 深度学习(DL) (opens new window)
    • 自然语言处理(NLP) (opens new window)
AI 导航 (opens new window)

Revin

首页
  • AI 工具

    • 绘图提示词工具 (opens new window)
    • ChatGPT 指令 (opens new window)
  • ChatGPT

    • ChatGP T介绍
    • ChatGPT API 中文开发手册
    • ChatGPT 中文调教指南
    • ChatGPT 开源项目
  • Midjourney

    • Midjourney 文档
  • Stable Diffusion

    • Stable Diffusion 文档
  • 其他

    • AIGC 热门文章
    • 账号合租 (opens new window)
    • 有趣的网站
  • Vue

    • Vue3前置
  • JAVA基础

    • Stream
    • Git
    • Maven
    • 常用第三方类库
    • 性能调优工具
    • UML系统建模
    • 领域驱动设计
    • 敏捷开发
    • Java 测试
    • 代码规范及工具
    • Groovy 编程
  • 并发编程&多线程

    • 并发编程
    • 高性能队列 Disruptor
    • 多线程并发在电商系统下的应用
  • 其他

    • 面试题
  • 消息中间中间件

    • Kafka
    • RabbitMQ
    • RocketMQ
  • 任务调度

    • Quartz
    • XXL-Job
    • Elastic-Job
  • 源码解析

    • Mybatis 高级使用
    • Mybatis 源码剖析
    • Mybatis-Plus
    • Spring Data JPA
    • Spring 高级使用
    • Spring 源码剖析
    • SpringBoot 高级使用
    • SpringBoot 源码剖析
    • Jdk 解析
    • Tomcat 架构设计&源码剖析
    • Tomcat Web应用服务器
    • Zookeeper 高级
    • Netty
  • 微服务框架

    • 分布式原理
    • 分布式集群架构场景化解决方案
    • Dubbo 高级使用
    • Dubbo 核心源码剖析
    • Spring Cloud Gateway
    • Nacos 实战应用
    • Sentinel 实战应用
    • Seata 分布式事务
  • 数据结构和算法的深入应用
  • 存储

    • 图和Neo4j
    • MongoDB
    • TiDB
    • MySQL 优化
    • MySQL 平滑扩容实战
    • MySQL 海量数据存储与优化
    • Elasticsearch
  • 缓存

    • Redis
    • Aerospike
    • Guava Cache
    • Tair
  • 文件存储

    • 阿里云 OSS 云存储
    • FastDF 文件存储
  • 基础

    • Linux 使用
    • Nginx 使用与配置
    • OpenResty 使用
    • LVS+Keepalived 高可用部署
    • Jekins
  • 容器技术

    • Docker
    • K8S
    • K8S
  • 01.全链路(APM)
  • 02.电商终极搜索解决方案
  • 03.电商亿级数据库设计
  • 04.大屏实时计算
  • 05.分库分表的深入实战
  • 06.多维系统下单点登录
  • 07.多服务之间分布式事务
  • 08.业务幂等性技术架构体系
  • 09.高并发下的12306优化
  • 10.每秒100W请求的秒杀架构体系
  • 11.集中化日志管理平台的应用
  • 12.数据中台配置中心
  • 13.每天千万级订单的生成背后痛点及技术突破
  • 14.红包雨的架构设计及源码实现
  • 人工智能

    • Python 笔记
    • Python 工具库
    • 人工智能(AI) 笔记
    • 人工智能(AI) 项目笔记
  • 大数据

    • Flink流处理框架
  • 加密区

    • 机器学习(ML) (opens new window)
    • 深度学习(DL) (opens new window)
    • 自然语言处理(NLP) (opens new window)
AI 导航 (opens new window)
  • Python介绍
  • 基础

  • 进阶

    • Python模块与包
    • Python面向对象
    • Python面向对象高级
    • Python高级语法
    • Python正则表达式
    • Python进程和线程
      • 1. 多任务的概念
        • 1.1 什么是多任务
        • 1.2 多任务的两种表现形式
        • 1.3 并发操作
        • 1.4 并行操作
      • 2. 进程的概念
        • 2.1 程序中实现多任务的方式
        • 2.2 进程的概念
        • 2.3 多进程的作用
        • ☆ 未使用多进程
        • ☆ 使用了多进程
      • 3. 多进程完成多任务
        • 3.1 多进程完成多任务
        • 3.2 通过进程类创建进程对象
        • 3.3 进程创建与启动的代码
        • 3.4 进程执行带有参数的任务
      • 4. 获取进程编号
        • 4.1 进程编号的作用
        • 4.2 两种进程编号
        • 4.3 获取当前进程编号
      • 5. 进程应用注意点
        • 5.1 进程间不共享全局变量
        • 5.2 主进程与子进程的结束顺序
        • ☆ 解决方案一:设置守护进程
        • ☆ 解决方案二:销毁子进程
      • 6. 线程的概念
        • 6.1 线程的概念
        • 6.2 为什么使用多线程?
        • 6.3 多线程的作用
        • ☆ 单线程执行
        • ☆ 多线程执行
      • 7. 多线程完成多任务
        • 7.1 多线程完成多任务
        • 7.2 线程创建与启动代码
        • 7.3 线程执行带有参数的任务
        • 7.4 主线程和子线程的结束顺序
        • ☆ 设置守护线程方式一
        • ☆ 设置守护线程方式二
        • 7.5 线程间的执行顺序
        • ☆ 获取当前线程信息
        • ☆ 线程间的执行顺序
        • 7.6 线程间共享全局变量
        • ☆ 线程间共享全局变量
        • 7.7 总结:进程和线程对比
        • ☆ 关系对比
        • ☆ 区别对比
        • ☆ 优缺点对比
    • Python闭包与装饰器
  • 其他

  • Python开源项目
  • Python
  • 进阶
Revin
2023-04-16
目录

Python进程和线程

# 1. 多任务的概念

# 1.1 什么是多任务

多任务是指在同一时间内执行多个任务。

例如: 现在电脑安装的操作系统都是多任务操作系统,可以同时运行着多个软件。

# 1.2 多任务的两种表现形式

① 并发

② 并行

# 1.3 并发操作

并发:在一段时间内交替去执行多个任务。

例如:对于单核cpu处理多任务,操作系统轮流让各个任务交替执行,假如:软件1执行0.01秒,切换到软件2,软件2执行0.01秒,再切换到软件3,执行0.01秒……这样反复执行下去 , 实际上每个软件都是交替执行的 . 但是,由于CPU的执行速度实在是太快了,表面上我们感觉就像这些软件都在同时执行一样 . 这里需要注意单核cpu是并发的执行多任务的。

image-20230421230540549

# 1.4 并行操作

并行:在一段时间内真正的同时一起执行多个任务。

对于多核cpu处理多任务,操作系统会给cpu的每个内核安排一个执行的任务,多个内核是真正的一起同时执行多个任务。这里需要注意多核cpu是并行的执行多任务,始终有多个任务一起执行。

# 2. 进程的概念

# 2.1 程序中实现多任务的方式

在Python中,想要实现多任务可以使用多进程来完成。

# 2.2 进程的概念

进程(Process)是资源分配的最小单位,它是操作系统进行资源分配和调度运行的基本单位,通俗理解:一个正在运行的程序就是一个进程。

例如:正在运行的qq , 微信等 他们都是一个进程。

注: 一个程序运行后至少有一个进程

# 2.3 多进程的作用

# ☆ 未使用多进程

image-20230421230728694

思考:

​ 图中是一个非常简单的程序 , 一旦运行hello.py这个程序 , 按照代码的执行顺序 , func_a函数执行完毕后才能执行func_b函数 . 如果可以让func_a和func_b同时运行 , 显然执行hello.py这个程序的效率会大大提升 .

image-20230421230741733

# ☆ 使用了多进程

image-20230421230803127

# 3. 多进程完成多任务

# 3.1 多进程完成多任务

① 导入进程包
import multiprocessing

② 通过进程类创建进程对象 
进程对象 = multiprocessing.Process() 

③ 启动进程执行任务
进程对象.start()
1
2
3
4
5
6
7
8

# 3.2 通过进程类创建进程对象

进程对象 = multiprocessing.Process([group [, target=任务名 [, name]]])
1

参数说明:

参数名 说明
target 执行的目标任务名,这里指的是函数名(方法名)
name 进程名,一般不用设置
group 进程组,目前只能使用None

# 3.3 进程创建与启动的代码

边听音乐边敲代码:

import multiprocessing
import time


def music():
    for i in range(3):
        print('听音乐...')
        time.sleep(0.2)


def coding():
    for i in range(3):
        print('敲代码...')
        time.sleep(0.2)

if __name__ == '__main__':
    music_process = multiprocessing.Process(target=music)
    coding_process = multiprocessing.Process(target=coding)

    music_process.start()
    coding_process.start()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# 3.4 进程执行带有参数的任务

Process([group [, target [, name [, args [, kwargs]]]]])
1

参数说明:

参数名 说明
args 以元组的方式给执行任务传参,args表示调用对象的位置参数元组,args=(1,2,'anne',)
kwargs 以字典方式给执行任务传参,kwargs表示调用对象的字典,kwargs={'name':'anne','age':18}

示例:args参数和kwargs参数的使用

import multiprocessing
import time


def music(num):
    for i in range(num):
        print('听音乐...')
        time.sleep(0.2)


def coding(count):
    for i in range(count):
        print('敲代码...')
        time.sleep(0.2)


music_process = multiprocessing.Process(target=music, args=(3, ))
coding_process = multiprocessing.Process(target=coding, kwargs={'count': 3})

music_process.start()
coding_process.start()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

示例:多个参数传递

import multiprocessing
import time


def music(num, name):
    for i in range(num):
        print(name)
        print('听音乐...')
        time.sleep(0.2)


def coding(count):
    for i in range(count):
        print('敲代码...')
        time.sleep(0.2)


if __name__ == '__main__':
    music_process = multiprocessing.Process(target=music, args=(3, '多任务开始'))
    coding_process = multiprocessing.Process(target=coding, kwargs={'count': 3})

    music_process.start()
    coding_process.start()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 4. 获取进程编号

# 4.1 进程编号的作用

当程序中进程的数量越来越多时 , 如果没有办法区分主进程和子进程还有不同的子进程 , 那么就无法进行有效的进程管理 , 为了方便管理实际上每个进程都是有自己编号的。

# 4.2 两种进程编号

① 获取当前进程编号

getpid()
1

② 获取当前进程的父进程ppid = parent pid

getppid()
1

# 4.3 获取当前进程编号

import os


def work():
    # 获取当前进程的编号
    print('work进程编号', os.getpid())
    # 获取父进程的编号
    print('work父进程的编号', os.getppid())
    

work()
1
2
3
4
5
6
7
8
9
10
11

示例:获取子进程编号

import multiprocessing
import time
import os


def music(num):
    print('music>> %d' % os.getpid())
    for i in range(num):
        print('听音乐...')
        time.sleep(0.2)


def coding(count):
    print('coding>> %d' % os.getpid())
    for i in range(count):
        print('敲代码...')
        time.sleep(0.2)


if __name__ == '__main__':
    music_process = multiprocessing.Process(target=music, args=(3, ))
    coding_process = multiprocessing.Process(target=coding, kwargs={'count': 3})

    music_process.start()
    coding_process.start()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

示例:获取父进程与子进程编号

import multiprocessing
import time
import os


def music(num):
    print('music>> %d' % os.getpid())
    print('music主进程>> %d' % os.getppid())
    for i in range(num):
        print('听音乐...')
        time.sleep(0.2)


def coding(count):
    print('coding>> %d' % os.getpid())
    print('music主进程>> %d' % os.getppid())
    for i in range(count):
        print('敲代码...')
        time.sleep(0.2)


if __name__ == '__main__':
    print('主进程>> %d' % os.getpid())
    music_process = multiprocessing.Process(target=music, args=(3, ))
    coding_process = multiprocessing.Process(target=coding, kwargs={'count': 3})

    music_process.start()
    coding_process.start()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# 5. 进程应用注意点

# 5.1 进程间不共享全局变量

实际上创建一个子进程就是把主进程的资源进行拷贝产生了一个新的进程,这里的主进程和子进程是互相独立的。

image-20230421230848414

示例:

import multiprocessing

my_list = []


def write_data():
    for i in range(3):
        my_list.append(i)
        print('add:', i)
    print(my_list)


def read_data():
    print('read_data', my_list)


if __name__ == '__main__':
    # 创建写入数据进程
    write_process = multiprocessing.Process(target=write_data)
    # 创建读取数据进程
    read_process = multiprocessing.Process(target=read_data)

    # 启动进程执行相关任务
    write_process.start()
    time.sleep(1)
    read_process.start()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

原理分析:

image-20230421230918223

三个进程分别操作的都是自己进程里面的全局变量my_list, 不会对其它进程里面的全局变量产生影响,所以进程之间不共享全局变量,只不过进程之间的全局变量名字相同而已,但是操作的不是同一个进程里面的全局变量。

知识点小结:

创建子进程会对主进程资源进行拷贝,也就是说子进程是主进程的一个副本,好比是一对双胞胎,之所以进程之间不共享全局变量,是因为操作的不是同一个进程里面的全局变量,只不过不同进程里面的全局变量名字相同而已。

# 5.2 主进程与子进程的结束顺序

image-20230421230933289

代码演示:

import multiprocessing
import time


# 工作函数
def work():
    for i in range(10):
        print('工作中...')
        time.sleep(0.2)


if __name__ == '__main__':
    # 创建子进程
    work_process = multiprocessing.Process(target=work)
    # 启动子进程
    work_process.start()

    # 延迟1s
    time.sleep(1)
    print('主进程执行完毕')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

执行结果:

image-20230421231001016

# ☆ 解决方案一:设置守护进程

import multiprocessing
import time


# 工作函数
def work():
    for i in range(10):
        print('工作中...')
        time.sleep(0.2)


if __name__ == '__main__':
    # 创建子进程
    work_process = multiprocessing.Process(target=work)
    # 设置守护主进程,主进程退出后子进程直接销毁,不再执行子进程中的代码
    work_process.daemon = True
    # 启动子进程
    work_process.start()

    # 延迟1s
    time.sleep(1)
    print('主进程执行完毕')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# ☆ 解决方案二:销毁子进程

import multiprocessing
import time


# 工作函数
def work():
    for i in range(10):
        print('工作中...')
        time.sleep(0.2)


if __name__ == '__main__':
    # 创建子进程
    work_process = multiprocessing.Process(target=work)
    # 启动子进程
    work_process.start()

    # 延迟1s
    time.sleep(1)
    
    # 让子进程直接销毁,表示终止执行, 主进程退出之前,把所有的子进程直接销毁就可以了
    work_process.terminate()
    
    print('主进程执行完毕')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

提示: 以上两种方式都能保证主进程退出子进程销毁

# 6. 线程的概念

# 6.1 线程的概念

在Python中,想要实现多任务还可以使用多线程来完成。

# 6.2 为什么使用多线程?

进程是分配资源的最小单位 , 一旦创建一个进程就会分配一定的资源 , 就像跟两个人聊QQ就需要打开两个QQ软件一样是比较浪费资源的 .

线程是程序执行的最小单位 , 实际上进程只负责分配资源 , 而利用这些资源执行程序的是线程 , 也就说进程是线程的容器 , 一个进程中最少有一个线程来负责执行程序 。同时线程自己不拥有系统资源,只需要一点儿在运行中必不可少的资源,但它可与同属一个进程的其它线程共享进程所拥有的全部资源 。这就像通过一个QQ软件(一个进程)打开两个窗口(两个线程)跟两个人聊天一样 , 实现多任务的同时也节省了资源。

# 6.3 多线程的作用

def func_a():
    print('任务A')


def func_b():
    print('任务B')


func_a()
func_b()
1
2
3
4
5
6
7
8
9
10

# ☆ 单线程执行

image-20230421231024511

# ☆ 多线程执行

image-20230421231044469

# 7. 多线程完成多任务

# 7.1 多线程完成多任务

① 导入线程模块
import threading

② 通过线程类创建线程对象
线程对象 = threading.Thread(target=任务名) 

② 启动线程执行任务
线程对象.start()
1
2
3
4
5
6
7
8
参数名 说明
target 执行的目标任务名,这里指的是函数名(方法名)
name 线程名,一般不用设置
group 线程组,目前只能使用None

# 7.2 线程创建与启动代码

单线程示例:

import time


def music():
    for i in range(3):
        print('听音乐...')
        time.sleep(0.2)


def coding():
    for i in range(3):
        print('敲代码...')
        time.sleep(0.2)


if __name__ == '__main__':
    music()
    coding()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

多线程示例:

import time
import threading


def music():
    for i in range(3):
        print('听音乐...')
        time.sleep(0.2)


def coding():
    for i in range(3):
        print('敲代码...')
        time.sleep(0.2)


if __name__ == '__main__':
    music_thread = threading.Thread(target=music)
    coding_thread = threading.Thread(target=coding)

    music_thread.start()
    coding_thread.start()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 7.3 线程执行带有参数的任务

参数名 说明
args 以元组的方式给执行任务传参
kwargs 以字典方式给执行任务传参
import time
import threading


def music(num):
    for i in range(num):
        print('听音乐...')
        time.sleep(0.2)


def coding(count):
    for i in range(count):
        print('敲代码...')
        time.sleep(0.2)


if __name__ == '__main__':
    music_thread = threading.Thread(target=music, args=(3, ))
    coding_thread = threading.Thread(target=coding, kwargs={'count': 3})

    music_thread.start()
    coding_thread.start()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 7.4 主线程和子线程的结束顺序

import time
import threading


def work():
    for i in range(10):
        print('work...')
        time.sleep(0.2)


if __name__ == '__main__':
    # 创建子进程
    work_thread = threading.Thread(target=work)
    # 启动线程
    work_thread.start()

    # 延时1s
    time.sleep(1)
    print('主线程执行完毕')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# ☆ 设置守护线程方式一

import time
import threading


def work():
    for i in range(10):
        print('work...')
        time.sleep(0.2)


if __name__ == '__main__':
    # 创建子线程并设置守护主线程
    work_thread = threading.Thread(target=work, daemon=True)
    # 启动线程
    work_thread.start()

    # 延时1s
    time.sleep(1)
    print('主线程执行完毕')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# ☆ 设置守护线程方式二

import time
import threading


def work():
    for i in range(10):
        print('work...')
        time.sleep(0.2)


if __name__ == '__main__':
    # 创建子线程
    work_thread = threading.Thread(target=work)
    # 设置守护主线程
    work_thread.setDaemon(True)
    # 启动线程
    work_thread.start()

    # 延时1s
    time.sleep(1)
    print('主线程执行完毕')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# 7.5 线程间的执行顺序

for i in range(5):
    sub_thread = threading.Thread(target=task)
    sub_thread.start()
1
2
3

思考:当我们在进程中创建了多个线程,其线程之间是如何执行的呢?按顺序执行?一起执行?还是其他的执行方式呢?

image-20230421231110898

答:线程之间的执行是无序的,验证

# ☆ 获取当前线程信息

# 通过current_thread方法获取线程对象
current_thread = threading.current_thread()

# 通过current_thread对象可以知道线程的相关信息,例如被创建的顺序
print(current_thread)
1
2
3
4
5

# ☆ 线程间的执行顺序

import threading
import time

def get_info():
    # 可以暂时先不加,查看效果
    time.sleep(0.5)
    current_thread = threading.current_thread()
    print(current_thread)


if __name__ == '__main__':
    # 创建子线程
    for i in range(10):
        sub_thread = threading.Thread(target=get_info)
        sub_thread.start()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

总结:线程之间执行是无序的,是由CPU调度决定某个线程先执行的。

# 7.6 线程间共享全局变量

# ☆ 线程间共享全局变量

多个线程都是在同一个进程中 , 多个线程使用的资源都是同一个进程中的资源 ,因此多线程间是共享全局变量

image-20230421231140500

示例代码:

import threading
import time


my_list = []


def write_data():
    for i in range(3):
        print('add:', i)
        my_list.append(i)
    print(my_list)


def read_data():
    print('read:', my_list)


if __name__ == '__main__':
    write_thread = threading.Thread(target=write_data)
    read_thread = threading.Thread(target=read_data)

    write_thread.start()
    time.sleep(1)
    read_thread.start()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

# 7.7 总结:进程和线程对比

# ☆ 关系对比

① 线程是依附在进程里面的,没有进程就没有线程。

② 一个进程默认提供一条线程,进程可以创建多个线程。

image-20230421231153966

# ☆ 区别对比

① 进程之间不共享全局变量

② 线程之间共享全局变量

③ 创建进程的资源开销要比创建线程的资源开销要大

④ 进程是操作系统资源分配的基本单位,线程是CPU调度的基本单位

# ☆ 优缺点对比

① 进程优缺点:

优点:可以用多核

缺点:资源开销大

② 线程优缺点

优点:资源开销小

缺点:不能使用多核

上次更新: 2025/04/03, 11:07:08
Python正则表达式
Python闭包与装饰器

← Python正则表达式 Python闭包与装饰器→

最近更新
01
tailwindcss
03-26
02
PaddleSpeech
02-18
03
whisper
02-18
更多文章>
Theme by Vdoing | Copyright © 2019-2025 跨境互联网 | 豫ICP备14016603号-5 | 豫公网安备41090002410995号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式