当前位置:网站首页>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 .
边栏推荐
- P7735-[noi2021] heavy and heavy edges [tree chain dissection, line segment tree]
- [001] [stm32] how to download STM32 original factory data
- Mysql数据库慢sql抓取与分析
- Viewing and verifying backup sets using dmrman
- Security xxE vulnerability recurrence (XXe Lab)
- DM8 backup set deletion
- The global and Chinese market of negative pressure wound therapy unit (npwtu) 2022-2028: Research Report on technology, participants, trends, market size and share
- Benefits of automated testing
- WPF effect Article 191 box selection listbox
- Scalpel like analysis of JVM -- this article takes you to peek into the secrets of JVM
猜你喜欢
Comprehensive ability evaluation system
颠覆你的认知?get和post请求的本质
Développement d'un module d'élimination des bavardages à clé basé sur la FPGA
综合能力测评系统
SSTI template injection explanation and real problem practice
KS008基于SSM的新闻发布系统
Network security - Security Service Engineer - detailed summary of skill manual (it is recommended to learn and collect)
绑定在游戏对象上的脚本的执行顺序
Solution to the problem that the root account of MySQL database cannot be logged in remotely
Chinese brand hybrid technology: there is no best technical route, only better products
随机推荐
Interface idempotency
【按鍵消抖】基於FPGA的按鍵消抖模塊開發
User datagram protocol UDP
How to standardize the deployment of automated testing?
2/12 didn't learn anything
潘多拉 IOT 开发板学习(HAL 库)—— 实验9 PWM输出实验(学习笔记)
Lora gateway Ethernet transmission
Record the pit of NETCORE's memory surge
关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
pd. to_ numeric
2/13 qaq~~ greed + binary prefix sum + number theory (find the greatest common factor of multiple numbers)
MySQL reads missing data from a table in a continuous period of time
记一次excel XXE漏洞
What is the difference between gateway address and IP address in tcp/ip protocol?
AcWing 243. A simple integer problem 2 (tree array interval modification interval query)
Stable Huawei micro certification, stable Huawei cloud database service practice
DM8 backup set deletion
C#(二十七)之C#窗体应用
Simple blog system
Oracle ORA error message