当前位置:网站首页>Understanding of processes and threads
Understanding of processes and threads
2022-07-06 06:09:00 【Wandering mage 12】
One 、 The concept of process and thread ( A word summary )
process : The program we run usually corresponds to one or more processes , Process is the basic unit of memory allocated by the operating system .
Threads : A process usually contains one or more threads , Threads are operating system allocations CPU The basic unit of .
Single threaded program ---> There is only one execution clue in our program ( The main thread )---> Create multiple threads ( There are multiple concurrent parts )
Technology exchange group :830709780
notes : A program has at least one process , A process has at least one thread ( The main thread ), Processes have independent memory units during execution , Multiple threads share memory , Thus greatly improving the efficiency of the program .
Two 、 Difference between process and thread
1. The way of operating system resource management is different , The process has a separate address space , After a process crashes, there will be a protection mode so that it will not affect other processes . Threads are not , Threads have their own stack and local variables , But there is no separate address space between threads , So a thread hanging may affect the whole process hanging .
2. The concurrency of processes is not as high as that of threads .
3. Each independent thread has an entry for the program to run 、 Exit of sequential execution sequence and program . But threads can't execute independently , Must exist in the application . Multiple thread execution control provided by the application .
4. For applications , Multithreading means that multiple execution parts can be executed at the same time . But for the operating system, it does not regard multiple threads as multiple independent applications , To achieve process scheduling and management and resource allocation
notes : Multithreading is easy to schedule , Effectively achieve concurrency . The overhead of memory is relatively small . Creating a thread is faster than creating a process .
3、 ... and 、 The concept of thread pools
When writing multithreaded programs , It must be noted that the creation and release of threads have a large overhead , And if you create too many threads , Scheduling switching between threads also has overhead , So the more threads, the better , The best use is to create several threads , Then reuse them .
Thread pool : First use a container , Create several threads in advance , When using threads, borrow a thread from the thread pool , After use , Do not release threads , Instead, put the thread back in the pool , Let threads be reused .
notes : Pooling technology is basically space for time .
Four 、 Code demonstration ( Call the third-party interface to download ( Multithreading and thread pooling ) picture )
# coding=utf8
# @time:2022/5/19 17:02
import time
from concurrent.futures import ThreadPoolExecutor
import requests
def down_picture(picture_url):
resp = requests.get(picture_url)
if resp.status_code == 200:
filename = picture_url[picture_url.rfind('/'):]
with open(f'images/{filename}', 'wb') as f:
f.write(resp.content)
def read_picture():
futures = []
with ThreadPoolExecutor(max_workers=10) as pool: # Thread pool , The biggest process 10
# threads = []
star = time.time()
format = 'png'
header = {
'User-Agent': 'Mozilla/5.0 (compatible; U; ABrowse 0.6; Syllable) AppleWebKit/420+ (KHTML, like Gecko)'}
data = {'key': '919928ff6d14cXXXXXXXXXXXXXX', 'url': 'https://www.hXXXXXXXXXXXXXX.com'} # Fill in the parameters according to the actual situation
for page in range(1, 6):
resp = requests.get('http://api.xxxxxxx.com', params=data, headers=header) # Fill in the parameters according to the actual situation
picture_list = resp.json()['newslist']
# print(picture_list)
for picture in picture_list:
picture_url = picture['picUrl']
# print(picture_url)
# down_picture(picture_url)
# Thread pool
f = pool.submit(down_picture, picture_url)
'''''''''
# Common multithreading methods
thd = threading.Thread(target=down_picture,args=(picture_url,))
threads.append(thd)
thd.start()
# print(threads)
# Wait for all the execution time of the child thread just started
for t in threads:
t.join()
'''''''''
for f in futures:
f.result()
end = time.time()
print(end - star) # 4.877447128295898
if __name__ == '__main__':
read_picture()
Download situation :
More secure sharing , Please pay attention to 【 Security info】 WeChat official account !
边栏推荐
- 【Postman】Collections配置运行过程
- Novice entry SCM must understand those things
- ContentType的作用
- What are the test sites for tunnel engineering?
- H3C S5820V2_ Upgrade method after stacking IRF2 of 5830v2 switch
- Request forwarding and redirection
- 【LeetCode】Day96-第一个唯一字符&赎金信&字母异位词
- 授予渔,从0开始搭建一个自己想要的网页
- Web service connector: Servlet
- Zoom through the mouse wheel
猜你喜欢
nodejs实现微博第三方登录
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
H3C防火墙RBM+VRRP 组网配置
IPv6 comprehensive experiment
Winter 2021 pat class B problem solution (C language)
Gtest之TEST宏的用法
Request forwarding and redirection
单元测试的意义
About PHP startup, mongodb cannot find the specified module
[untitled]
随机推荐
continue和break的区别与用法
PAT(乙级)2022年夏季考试
GTSAM中ISAM2和IncrementalFixedLagSmoother说明
Accélération de la lecture vidéo de l'entreprise
H3C V7版本交换机配置IRF
C language bubble sort
公司視頻加速播放
Interface test: what are the components of the URL in fiddler
在线问题与离线问题
曼哈顿距离和-打印菱形
MIT6.s081-2020 Lab2 System Calls
Web service connector: Servlet
LTE CSFB process
Overview of three core areas of Mathematics: geometry
公司视频加速播放
Dynamic programming -- knapsack problem
授予渔,从0开始搭建一个自己想要的网页
Eigen sparse matrix operation
10M25DCF484C8G(FPGA) AMY-6M-0002 BGA GPS模块
Title 1093: character reverse order