当前位置:网站首页>关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
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、并行的关键是你有同时处理多个任务的能力。
十、串行
简单来说就是一次只能做一件事情,而且还得按照顺序依次执行,后面的代码段必须等到前面代码段的任务执行完毕后才能执行。


边栏推荐
- Class A, B, C networks and subnet masks in IPv4
- Chinese brand hybrid technology: there is no best technical route, only better products
- 【可调延时网络】基于FPGA的可调延时网络系统verilog开发
- After five years of testing in byte, I was ruthlessly dismissed in July, hoping to wake up my brother who was paddling
- 使用JS完成一个LRU缓存
- User perceived monitoring experience
- Esp32 (based on Arduino) connects the mqtt server of emqx to upload information and command control
- Global and Chinese markets for medical gas manifolds 2022-2028: Research Report on technology, participants, trends, market size and share
- Ethernet port &arm & MOS &push-pull open drain &up and down &high and low sides &time domain and frequency domain Fourier
- Mapping between QoE and KQI
猜你喜欢

【FPGA教程案例11】基于vivado核的除法器设计与实现

Database, relational database and NoSQL non relational database

MySQL master-slave replication

KS008基于SSM的新闻发布系统

C form application of C (27)

mysql关于自增长增长问题
![[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
![[001] [stm32] how to download STM32 original factory data](/img/5a/02d87fe1409a9427180ecefb8326c6.jpg)
[001] [stm32] how to download STM32 original factory data

Facebook and other large companies have leaked more than one billion user data, and it is time to pay attention to did

C#(二十九)之C#listBox checkedlistbox imagelist
随机推荐
阿里测试师用UI自动化测试实现元素定位
Global and Chinese market of rubber wheel wedges 2022-2028: Research Report on technology, participants, trends, market size and share
C (XXIX) C listbox CheckedListBox Imagelist
Fundamentals of SQL database operation
Facebook等大廠超十億用戶數據遭泄露,早該關注DID了
登录mysql输入密码时报错,ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES
Esp32 (based on Arduino) connects the mqtt server of emqx to upload information and command control
/usr/bin/gzip: 1: ELF: not found/usr/bin/gzip: 3: : not found/usr/bin/gzip: 4: Syntax error:
Redis (replicate dictionary server) cache
The global and Chinese market of negative pressure wound therapy unit (npwtu) 2022-2028: Research Report on technology, participants, trends, market size and share
[optimization model] Monte Carlo method of optimization calculation
How does technology have the ability to solve problems perfectly
Scalpel like analysis of JVM -- this article takes you to peek into the secrets of JVM
Oracle ORA error message
/usr/bin/gzip: 1: ELF: not found/usr/bin/gzip: 3: : not found/usr/bin/gzip: 4: Syntax error:
[Key shake elimination] development of key shake elimination module based on FPGA
Take you to wechat applet development in 3 minutes
Differential GPS RTK thousand search
在 .NET 6 中使用 Startup.cs 更简洁的方法
How to modify field constraints (type, default, null, etc.) in a table