当前位置:网站首页>Multitask programming
Multitask programming
2022-07-26 00:17:00 【Ding Jiaxiong】
3. Multitask programming
List of articles
Multitasking refers to executing multiple tasks at the same time , for example : Now the operating systems installed on computers are multitasking operating systems , Can run multiple software at the same time
3.1 Multi task execution mode
3.1.1 Concurrent
- Alternate tasks over a period of time .
3.1.2 parallel
- For multi-core cpu Dealing with multitasking , The operating system will give cpu Each kernel of is scheduled to have an executing software
- Multiple cores are really executing software together . Here we need to pay attention to multi-core cpu It's multitasking in parallel , There are always multiple software executing together
3.2 process
A running program or software is a process , It is the basic unit of resource allocation in the operating system
At least one process after a program runs , A process has one thread by default , Multiple threads can be created in the process , Threads are attached to processes , No process, no thread
Multiple processes can accomplish multiple tasks
3.2.1 The use of multiprocessing
Process package
- multiprocessing
Process Description of the process class Process([group [, target [, name [, args [, kwargs]]]]])
- group: Specify process group , At present, you can only use None
- target: Name of the target task to be executed
- name: Process name
- args: Pass parameters to the execution task in tuple mode
- kwargs: Pass parameters to the execution task in a dictionary
Process Common methods of creating instance objects
- start(): Start subprocess instance ( Create child process )
- join(): Wait for subprocess execution to finish
- terminate(): Whether the task is completed or not , Terminate child process now
3.2.2 Get the process number
- Get the current process number os.getpid()
- Get the current parent process number os.getppid()
- Get the process number to view the relationship between parent and child processes
3.2.3 Processes perform tasks with parameters
args Parameters
Tuples
- Tuple mode parameter transmission must be consistent with the order of parameters .
kwargs Parameters
Dictionaries
- Dictionary mode refers to... In the dictionary key Be sure to keep consistent with the parameter name .
3.2.4 Be careful
Global variables are not shared between processes
- Creating a child process copies the resources of the main process , That is, the child process is a copy of the main process , Like a pair of twins , The reason why global variables are not shared between processes , Because the global variables in the same process are not operated , It's just that the names of global variables in different processes are the same .
The main process will wait for all child processes to finish executing
How to make the main process exit and the child process destroy , Don't let the main process wait for the child process to execute
Guard the main process
- Subprocess object .daemon = True
Destroy subprocesses
- Subprocess object .terminate()
Execution between processes is also out of order
- Determined by the operating system scheduling
3.3 Threads
Thread is cpu The basic unit of dispatch , Each process has at least one thread , And this thread is what we usually call the main thread
Multithreading can also accomplish multitasking
3.3.1 Use of multithreading
Thread module
- threading
Thread class Thread Parameter description
Thread([group [, target [, name [, args [, kwargs]]]]])
- group: Thread group , At present, you can only use None
- target: Name of the target task to be executed
- args: Pass parameters to the execution task in the form of tuples
- kwargs: Pass parameters to the execution task in a dictionary
- name: The thread of , In general, there is no need to set
sub_thread = threading.Thread(target= Task name )
Start thread
- start Method
3.3.2 Threads perform tasks with parameters
args Parameters
- Tuple mode parameter transmission must be consistent with the order of parameters .
kwargs Parameters
- Dictionary mode refers to... In the dictionary key Be sure to keep consistent with the parameter name .
3.3.3 Be careful
Execution between threads is out of order
- from cpu Scheduling decision
The main thread will wait for all child threads to finish executing
How to make the main thread exit and the sub thread destroy , Don't let the main thread wait for the sub thread to execute
Guard the main thread
- threading.Thread(target=show_info, daemon=True)
- Process object .setDaemon(True)
Sharing global variables between threads
There is an error in sharing global variable data between processes
terms of settlement : Thread synchronization
Thread waiting join()
- th1.start()
th1.join()
th2.start()
- th1.start()
The mutex
3.4 The mutex
Lock shared data , Ensure that only one thread can operate at the same time
3.4.1 Use
Create a lock
- mutex = threading.Lock()
locked
- mutex.acquire()
The lock ( Liberation lock )
- mutex.release()
3.4.2 Be careful
- acquire and release The code between methods can only have one thread to operate at the same time
- If you're calling acquire Method time Other threads already use this mutex , So at this time acquire Method will be blocked , The mutex cannot be locked again until it is released
Coupled with mutex, multitasking instantly becomes a single task , Performance will degrade , That is to say, only one thread can execute at the same time
3.5 Deadlock
The situation of waiting for the other party to release the lock is deadlock
result
- Will cause the application to stop responding , No more tasks
3.5.1 Avoid deadlock
- Release the lock in the right place
3.6 Process versus thread
3.6.1 Relationship comparison
- Threads are attached to processes , No process, no thread .
- A process provides a thread by default , Processes can create multiple threads .
3.6.2 Difference and contrast
- Global variables are not shared between processes
- Sharing global variables between threads , But we should pay attention to the problem of resource competition , terms of settlement : Mutex or thread synchronization
- The resource cost of creating a process is greater than that of creating a thread
- Process is the basic unit of operating system resource allocation , Thread is CPU The basic unit of dispatch
- Thread cannot execute independently , Must be in the process of existence
- Multi process development is more stable than single process and multi thread development
3.6.3 Comparison of advantages and disadvantages
- Process advantages and disadvantages :
advantage : You can use multi-core
shortcoming : Resources are expensive - Advantages and disadvantages of threads :
advantage : The cost of resources is small
shortcoming : Multi core cannot be used
边栏推荐
- VMware ESXI7.0版本的安装与配置
- "Demons dance", is the bull market over? 2021-05-13
- Leetcode high frequency question 66. add one, give you an array to represent numbers, then add one to return the result
- [brother hero July training] day 24: linear tree
- 初阶C语言 - 分支语句(if、switch)
- Unified handling of global exceptions
- 解决不挂载数据的页面刷新
- Js理解之路:Js常见的6中继承方式
- How to make your JS code more beautiful
- Binary tree -- 222. Number of nodes of a complete binary tree
猜你喜欢

【论文笔记】—目标姿态估计—EPro-PnP—2022-CVPR

Binary tree -- 222. Number of nodes of a complete binary tree

对“DOF: A Demand-oriented Framework for ImageDenoising“的理解

栈的表示和实现(C语言)

SHIB(柴犬币)一月涨幅数百倍,百倍币需具备哪些核心要素?2021-05-09

Binary tree - 617. Merge binary tree

本轮牛市还能持续多久?|疑问解答 2021-05-11

如何用120行代码,实现一个交互完整的拖拽上传组件?

Bond network card mode configuration

二进制表示--2的幂
随机推荐
MySQL——数据库日志
How long can this bull market last Answers to questions 2021-05-11
Backtracking - 77. combination
FreeRTOS个人笔记-互斥量
牛市还将继续,拿好手里的币 2021-05-08
“动物币”凶猛,陷阱还是机遇?2021-05-12
06_ue4进阶_使用地形工具设置大地图
牛市还没有结束,还有下半场 2021-05-18
获得JD商品详情原数据 API
Binary tree 101. Symmetric binary tree
如何用120行代码,实现一个交互完整的拖拽上传组件?
软件测试同行评审到底是什么?
Leetcode shahutong series -- 63. Different paths II
C language actual combat guessing game
This time, thoroughly understand promise principle
Backtracking - 17. Letter combinations of phone numbers
After using MQ message oriented middleware, I began to regret
这一次,彻底弄懂 Promise 原理
MySQL 索引使用有哪些注意事项呢?(从六个方面回答)
Appium中控件元素封装类梳理