当前位置:网站首页>Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization
Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization
2022-07-06 04:09:00 【YZL40514131】
One 、 process
1、 Open a browser , Is to start a browser process .
2、 A running program ( Code ) It's a process , No running code is called a program .
3、 Process is the smallest unit of system resource allocation , Processes have their own independent memory space , All inter process data is not shared , Spending big .
4、 A process is the basic unit of executing a task , It is also the basic unit for the operating system to perform tasks . The process contains a collection of program instructions and related resources .
5、 Create a process : Import first multiprocessing Medium Process
6、 Do not share global variables between processes
p = Process(target=XXX,args=(tuple,),kwargs={key:value})
target = XXX Specified task function , Don't have to add (),
args=(tuple,)
kwargs={key:value} Parameters passed to the task function
import os
from multiprocessing import Process
import time
def pro_func(name):
for i in range(5):
print(f' Subprocesses {
name+str(i)} Start execution ')
time.sleep(2)
print(f' Subprocesses {
name+str(i)} End to perform ')
if __name__ == '__main__':
p=Process(target=pro_func,args=('kobe',))
p.start()
print(' The execution of the main process is over ')
Two 、 Threads
1、 In a process , At least one thread , This thread is the main thread of the current process ,
2、 Thread is the basic unit of executing tasks in a process
3、 Thread cannot exist independently , Dependent processes exist
4、 Multiple threads share memory ( Data sharing , Share global variables ), Thus greatly improving the efficiency of the program .
5、 Multithreading may cause data chaos or even deadlock
6、 Create thread : Import first threading Medium Thread
import threading
import time
def thread():
for i in range(5):
print(f' Sub thread {
i} Start execution ')
time.sleep(2)
print(f' Sub thread {
i} End to perform ')
if __name__ == '__main__':
t=threading.Thread(target=thread)
t.start()
print(' Main thread execution completed ')
3、 ... and 、 coroutines
1、 Coroutine is a lightweight thread in user mode , The scheduling of the process is completely controlled by the user ; The benefit of this is that the performance has been greatly improved , It doesn't consume resources like thread switching .
2、 Synergetics generally use gevent library
3、 A thread can also have multiple coroutines
Four 、 Sync
Multiple tasks are executed in sequence , Only after one execution can the next be executed .
5、 ... and 、 asynchronous
Relative to synchronization , Synchronization is performed sequentially , Asynchrony is independent of each other , Continue to do your own thing while waiting for an event , Don't wait for this event to complete before you work . Threads are a way to achieve asynchrony , Asynchrony means that the main thread calling a method does not need to wait for another thread to complete synchronously , So that the main thread can do other things .
6、 ... and 、 Blocking
If the caller gets stuck , The caller cannot continue to execute , That is, the caller is blocking .
For example, the blocking function when multithreading executes tasks t.join(), The main route is blocked first , Wait for the sub thread to finish executing , End of main thread execution .
import threading
import time
def thread():
for i in range(5):
print(f' Sub thread {
i} Start execution ')
time.sleep(2)
print(f' Sub thread {
i} End to perform ')
if __name__ == '__main__':
t=threading.Thread(target=thread)
t.start()
t.join()
print(' Main thread execution completed ')
7、 ... and 、 Non blocking
If it doesn't get stuck , Can continue to execute , That means non blocking .
8、 ... and 、 Concurrent
1、 Only one instruction can be executed at the same time , But multiple process instructions are executed in rapid rotation , It has the effect of multiple processes executing at the same time , But at the micro level, it is not carried out at the same time , Just divide the time into periods , Make multiple processes execute alternately quickly .
2、 The key to concurrency is your ability to handle multiple tasks , Not necessarily at the same time .
Nine 、 parallel
1、 At the same time , There are multiple instructions executing on multiple processors at the same time . So whether from the micro or from the macro point of view , Both are carried out together .
2、 The key to parallelism is your ability to handle multiple tasks at the same time .
Ten 、 Serial
Simply put, you can only do one thing at a time , And it has to be executed in sequence , The later code segment can only be executed after the task of the previous code segment is executed .
边栏推荐
- MySQL reads missing data from a table in a continuous period of time
- Introduction to data types in MySQL
- Prime Protocol宣布在Moonbeam上的跨链互连应用程序
- Basic knowledge of binary tree, BFC, DFS
- Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
- DM8 backup set deletion
- ESP32_ FreeRTOS_ Arduino_ 1_ Create task
- WPF效果第一百九十一篇之框选ListBox
- 10 exemples les plus courants de gestion du trafic istio, que savez - vous?
- 80% of the diseases are caused by bad living habits. There are eight common bad habits, which are both physical and mental
猜你喜欢
Path of class file generated by idea compiling JSP page
在 .NET 6 中使用 Startup.cs 更简洁的方法
KS008基于SSM的新闻发布系统
[disassembly] a visual air fryer. By the way, analyze the internal circuit
颠覆你的认知?get和post请求的本质
C#(二十八)之C#鼠标事件、键盘事件
Yyds dry goods inventory web components series (VII) -- life cycle of custom components
C mouse event and keyboard event of C (XXVIII)
《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动
10个 Istio 流量管理 最常用的例子,你知道几个?
随机推荐
/usr/bin/gzip: 1: ELF: not found/usr/bin/gzip: 3: : not found/usr/bin/gzip: 4: Syntax error:
C language -- structs, unions, enumerations, and custom types
Cf464e the classic problem [shortest path, chairman tree]
Proof of Stirling formula
Stack and queue
Path of class file generated by idea compiling JSP page
math_极限&微分&导数&微商/对数函数的导函数推导(导数定义极限法)/指数函数求导公式推导(反函数求导法则/对数求导法)
math_ Derivative function derivation of limit & differential & derivative & derivative / logarithmic function (derivative definition limit method) / derivative formula derivation of exponential functi
ESP32_ FreeRTOS_ Arduino_ 1_ Create task
Script lifecycle
Unity中几个重要类
Global and Chinese markets for medical gas manifolds 2022-2028: Research Report on technology, participants, trends, market size and share
Basic knowledge of binary tree, BFC, DFS
【FPGA教程案例12】基于vivado核的复数乘法器设计与实现
登录mysql输入密码时报错,ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES
2/13 review Backpack + monotonic queue variant
Interface idempotency
如何修改表中的字段约束条件(类型,default, null等)
10个 Istio 流量管理 最常用的例子,你知道几个?
Execution order of scripts bound to game objects