当前位置:网站首页>关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
2022-07-06 04:04:00 【YZL40514131】
一、进程
1、打开一个浏览器就是,就是启动了一个浏览器进程。
2、一个运行的程序(代码)就是一个进程,没有运行的代码叫程序。
3、进程是系统资源分配的最小单位,进程拥有自己独立的内存空间,所有进程间数据不共享,开销大。
4、进程是执行任务的基本单元,也是操作系统执行任务的基本单元。进程中包含了程序指令和相关资源的集合。
5、创建进程: 首先要导入multiprocessing中的Process
6、进程间不共享全局变量
p = Process(target=XXX,args=(tuple,),kwargs={key:value})
target = XXX 指定的任务函数,不用加(),
args=(tuple,)
kwargs={key:value}给任务函数传递的参数
import os
from multiprocessing import Process
import time
def pro_func(name):
for i in range(5):
print(f'子进程{
name+str(i)}开始执行')
time.sleep(2)
print(f'子进程{
name+str(i)}结束执行')
if __name__ == '__main__':
p=Process(target=pro_func,args=('kobe',))
p.start()
print('主进程执行结束')
二、线程
1、在一个进程中,至少有一个线程,这个线程就是当前进程的主线程,
2、线程是进程中执行任务的基本单元
3、线程不能独立存在,依赖进程存在
4、而多个线程共享内存(数据共享,共享全局变量),从而极大地提高了程序的运行效率。
5、多线程执行任务会出现数据混乱的问题甚至是死锁
6、创建线程: 首先要导入threading中的Thread
import threading
import time
def thread():
for i in range(5):
print(f'子线程{
i}开始执行')
time.sleep(2)
print(f'子线程{
i}结束执行')
if __name__ == '__main__':
t=threading.Thread(target=thread)
t.start()
print('主线程执行完毕')
三、协程
1、协程是一种用户态的轻量级线程,协程的调度完全由用户控制;这样带来的好处就是性能得到了很大的提升,不会像线程切换那样消耗资源。
2、协程一般是使用gevent库
3、一个线程也可以拥有多个协程

四、同步
多个任务之间有先后顺序执行,一个执行完下个才能执行。
五、异步
和同步相对,同步是顺序执行,而异步是彼此独立,在等待某个事件的过程中继续做自己的事,不要等待这一事件完成后再工作。线程是实现异步的一个方式,异步是让调用方法的主线程不需要同步等待另一个线程的完成,从而让主线程干其他事情。
六、阻塞
如果卡住了调用者,调用者不能继续往下执行,就是说调用者阻塞了。
例如多线程执行任务时的阻塞函数t.join(),主线程先处于阻塞状态,等待子线程执行完毕之后,主线程执行结束。
import threading
import time
def thread():
for i in range(5):
print(f'子线程{
i}开始执行')
time.sleep(2)
print(f'子线程{
i}结束执行')
if __name__ == '__main__':
t=threading.Thread(target=thread)
t.start()
t.join()
print('主线程执行完毕')
七、非阻塞
如果不会卡住,可以继续执行,就是说非阻塞的。
八、并发
1、指在同一时刻只能有一条指令执行,但多个进程指令被快速的轮换执行,使得在宏观上具有多个进程同时执行的效果,但在微观上并不是同时执行的,只是把时间分成若干段,使多个进程快速交替的执行。
2、并发的关键是你有处理多个任务的能力,不一定要同时。
九、并行
1、指在同一时刻,有多条指令在多个处理器上同时执行。所以无论从微观还是从宏观来看,二者都是一起执行的。
2、并行的关键是你有同时处理多个任务的能力。
十、串行
简单来说就是一次只能做一件事情,而且还得按照顺序依次执行,后面的代码段必须等到前面代码段的任务执行完毕后才能执行。


边栏推荐
- Record the pit of NETCORE's memory surge
- Flask learning and project practice 8: introduction and use of cookies and sessions
- [adjustable delay network] development of FPGA based adjustable delay network system Verilog
- 记一次excel XXE漏洞
- C#(三十一)之自定义事件
- 51nod 1130 n factorial length V2 (Stirling approximation)
- 综合能力测评系统
- Ks008 SSM based press release system
- [introduction to Django] 11 web page associated MySQL single field table (add, modify, delete)
- Why do you want to start pointer compression?
猜你喜欢
![[FPGA tutorial case 11] design and implementation of divider based on vivado core](/img/39/f337510c2647d365603a8485583a20.png)
[FPGA tutorial case 11] design and implementation of divider based on vivado core

Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage

Maxay paper latex template description

在 .NET 6 中使用 Startup.cs 更简洁的方法

JVM的手术刀式剖析——一文带你窥探JVM的秘密

How to standardize the deployment of automated testing?

Blue Bridge Cup - day of week

Flask learning and project practice 8: introduction and use of cookies and sessions

What is the difference between gateway address and IP address in tcp/ip protocol?

C mouse event and keyboard event of C (XXVIII)
随机推荐
Stack and queue
Custom event of C (31)
Leetcode32 longest valid bracket (dynamic programming difficult problem)
Développement d'un module d'élimination des bavardages à clé basé sur la FPGA
Detailed explanation of serialization and deserialization
DM8 backup set deletion
WPF effect Article 191 box selection listbox
在 .NET 6 中使用 Startup.cs 更简洁的方法
Blue Bridge Cup - Castle formula
What is the difference between gateway address and IP address in tcp/ip protocol?
综合能力测评系统
Solution to the problem that the root account of MySQL database cannot be logged in remotely
C#(三十)之C#comboBox ListView treeView
有条件地 [JsonIgnore]
Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did
Python book learning notes - Chapter 09 section 01 create and use classes
MySQL reads missing data from a table in a continuous period of time
Fundamentals of SQL database operation
记一次excel XXE漏洞
Do you know cookies, sessions, tokens?