当前位置:网站首页>关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
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、并行的关键是你有同时处理多个任务的能力。
十、串行
简单来说就是一次只能做一件事情,而且还得按照顺序依次执行,后面的代码段必须等到前面代码段的任务执行完毕后才能执行。
边栏推荐
- Custom event of C (31)
- Conditionally [jsonignore]
- MySQL 中的数据类型介绍
- What is the difference between gateway address and IP address in tcp/ip protocol?
- Global and Chinese markets for otolaryngology devices 2022-2028: Research Report on technology, participants, trends, market size and share
- [practical exercise] face location model based on skin color
- [practice] mathematics in lottery
- Ks008 SSM based press release system
- Facebook and other large companies have leaked more than one billion user data, and it is time to pay attention to did
- Determine which week of the month the day is
猜你喜欢
[Zhao Yuqiang] deploy kubernetes cluster with binary package
ESP32(基于Arduino)连接EMQX的Mqtt服务器上传信息与命令控制
Record an excel xxE vulnerability
在 .NET 6 中使用 Startup.cs 更简洁的方法
math_极限&微分&导数&微商/对数函数的导函数推导(导数定义极限法)/指数函数求导公式推导(反函数求导法则/对数求导法)
2.13 weekly report
C (XXIX) C listbox CheckedListBox Imagelist
Align items and align content in flex layout
User datagram protocol UDP
Exchange bottles (graph theory + thinking)
随机推荐
MySql數據庫root賬戶無法遠程登陸解决辦法
ESP32_ FreeRTOS_ Arduino_ 1_ Create task
Yyds dry goods inventory hcie security Day11: preliminary study of firewall dual machine hot standby and vgmp concepts
[disassembly] a visual air fryer. By the way, analyze the internal circuit
Network security - Security Service Engineer - detailed summary of skill manual (it is recommended to learn and collect)
Record the pit of NETCORE's memory surge
Cf464e the classic problem [shortest path, chairman tree]
Indicator system of KQI and KPI
Align items and align content in flex layout
Class A, B, C networks and subnet masks in IPv4
Why do you want to start pointer compression?
在字节做测试5年,7月无情被辞,想给划水的兄弟提个醒
MySQL transaction isolation level
10个 Istio 流量管理 最常用的例子,你知道几个?
Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did
Ks003 mall system based on JSP and Servlet
Redis (replicate dictionary server) cache
/usr/bin/gzip: 1: ELF: not found/usr/bin/gzip: 3: : not found/usr/bin/gzip: 4: Syntax error:
In Net 6 CS more concise method
WPF效果第一百九十一篇之框选ListBox