当前位置:网站首页>Day41 process pool and thread pool
Day41 process pool and thread pool
2022-06-11 09:18:00 【lwj_ 07】
Thread pool
from concurrent.futures import ThreadPoolExecutor
import time
pool = ThreadPoolExecutor(5) # There are only five threads fixed in the pool ( There are only five people in a shop There will always be only these five people Save resources )
# Numbers can be passed in parentheses If not, the current computer will be opened by default cpu Five times the number of threads
def task(n):
print(n)
time.sleep(2)
return n**n
'''
How to submit the task
Sync : Wait for the return result of the task in place after submitting the task Do nothing in the meantime
asynchronous : Do not wait for the return result of the task after submitting the task Continue to execute the command
How to get the asynchronous return result ????( Asynchronous callback )
submit Method has a return value
'''
# pool.submit(task,1) # Submit a task to the pool submit For asynchronous commit
# print(' Lord ')
for i in range(20): # Submit... To the pool 20 A mission ( Five waiters serve )
res = pool.submit(task,i) # <Future at 0x2157277d3a0 state=pending> Back to a Future object # Future There is one result Method
print(res.result())
'''
print(res.result())
Discover that the program changes from concurrent to serial
Why does the task print None ???
>>> When tasks task Back to what res.result() You can get the returned result of the asynchronously submitted task
'''Print the results result Method >>>> Synchronous commit

Can we make this 20 After each task gets up, the result can be obtained slowly instead of waiting in place for each asynchronous task
from concurrent.futures import ThreadPoolExecutor
import time
pool = ThreadPoolExecutor(5) # There are only five threads fixed in the pool ( There are only five people in a shop There will always be only these five people Save resources )
# Numbers can be passed in parentheses If not, the current computer will be opened by default cpu Five times the number of threads
def task(n):
print(n)
time.sleep(2)
return n**n
r_list = []
for i in range(20): # Submit... To the pool 20 A mission ( Five waiters serve )
res = pool.submit(task,i) # <Future at 0x2157277d3a0 state=pending> Back to a Future object # Future There is one result Method
r_list.append(res)
# print(res.result()) # result Method Synchronous commit
for t in r_list:
print('>>>:',t.result()) # The order of getting them is orderly

How to use other methods to replace result The callback mechanism ???
from concurrent.futures import ThreadPoolExecutor
import time
pool = ThreadPoolExecutor(5) # There are only five threads fixed in the pool ( There are only five people in a shop There will always be only these five people Save resources )
# Numbers can be passed in parentheses If not, the current computer will be opened by default cpu Five times the number of threads
def task(n):
print(n)
time.sleep(2)
return n**n
def call_back(n):
print('call_back >>> :',n) # # <Future at 0x2157277d3a0 state=pending> Back to a Future object # Future There is one result Method
print('call_back >>> :', n.result()) # Get the asynchronous callback result
for i in range(20): # Submit... To the pool 20 A mission ( Five waiters serve )
res = pool.submit(task,i).add_done_callback(call_back)
# r_list.append(res)
# print(res.result()) # result Method Synchronous commit
# for t in r_list:
# print('>>>:',t.result()) # The order of getting them is orderly
How to wait for all the tasks in the thread pool to be executed before proceeding ? Which means wait (1,20) Execute after the execution is completed >>> shut.down() Method
from concurrent.futures import ThreadPoolExecutor
import time
pool = ThreadPoolExecutor(5) # There are only five threads fixed in the pool ( There are only five people in a shop There will always be only these five people Save resources )
# Numbers can be passed in parentheses If not, the current computer will be opened by default cpu Five times the number of threads
def task(n):
print(n)
time.sleep(2)
return n**n
r_list = []
for i in range(20): # Submit... To the pool 20 A mission ( Five waiters serve )
res = pool.submit(task,i) # <Future at 0x2157277d3a0 state=pending> Back to a Future object # Future There is one result Method
r_list.append(res)
# print(res.result()) # result Method Synchronous commit
pool.shutdown()
for t in r_list:
print('>>>:',t.result()) # The order of getting them is orderly

os.getpid() see ip Number ( Five threads ( process ) Loop back and forth )
summary
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
pool = ProcessPoolExecutor(5)
pool.submit(task, i).add_done_callback(call_back)
Basic usage summary
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import time
import os
# pool = ThreadPoolExecutor(5) # There are only five threads fixed in the pool
# Numbers can be passed in parentheses If not, the current computer will be opened by default cpu Five times the number of threads
pool = ProcessPoolExecutor(5)
# Numbers can be passed in parentheses If not, the current computer will be opened by default cpu Number of processes
"""
After the pool is built There will be five fixed threads
There will be no repeated creation and destruction of these five threads
After the pool is built There will be several fixed processes
There will be no repeated creation and destruction in these processes
The use of the pool is very simple
You just need to submit the tasks you need to do to the pool Someone will automatically serve you
"""
def task(n):
print(n,os.getpid())
time.sleep(2)
return n**n
def call_back(n):
print('call_back>>>:',n.result())
"""
How to submit the task
Sync : After submitting the task, wait in place for the return result of the task Do nothing during
asynchronous : After submitting the task, do not wait for the return result of the task Execute continue to execute
How to get the returned results ???
Asynchronously submit the returned result of the task It should be obtained through the callback mechanism
Callback mechanism
It is equivalent to binding a time bomb to each asynchronous task
As soon as the mission results, it will trigger an explosion
"""
if __name__ == '__main__':
# pool.submit(task, 1) # Submit a task to the pool Asynchronous submission
# print(' Lord ')
# t_list = []
for i in range(20): # Submit... To the pool 20 A mission
# res = pool.submit(task, i) # <Future at 0x100f97b38 state=running>
res = pool.submit(task, i).add_done_callback(call_back)
# print(res.result()) # result Method Synchronous commit
# t_list.append(res)
# Wait until all the tasks in the thread pool have been executed before continuing
# pool.shutdown() # Close thread pool Wait for all tasks in the thread pool to run
# for t in t_list:
# print('>>>:',t.result()) # It must be orderly
"""
The program has concurrency and becomes serial
Why does the task print None
res.result() What you get is the return result of the asynchronously submitted task
"""边栏推荐
- 报错[error] input tesnor exceeds available data range [NeuralNetwork(3)] [error] Input tensor ‘0‘ (0)
- Version mismatch between installed deeply lib and the required one by the script
- 远程办公最佳实践及策略
- 报错Output image is bigger(1228800B) than maximum frame size specified in properties(1048576B)
- 1493. the longest subarray with all 1 after deleting an element
- 2130. maximum twin sum of linked list
- 从企业评价的方历来看ERP软件成功与失利
- Fabric. JS dynamically set font size
- OpenCV CEO教你用OAK(五):基于OAK-D和DepthAI的反欺骗人脸识别系统
- 剑指 Offer II 041. 滑动窗口的平均值
猜你喜欢

OpenCV CEO教你用OAK(五):基于OAK-D和DepthAI的反欺骗人脸识别系统

报错ModularNotFoundError: No module named ‘find_version’

CUMT learning diary - theoretical analysis of uCOSII - Textbook of Renzhe Edition

Award winning survey streamnational sponsored 2022 Apache pulsar user questionnaire

Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC

Augmented reality experiment IV of Shandong University

Talk about how to customize data desensitization
![报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s](/img/54/f42146ae649836fe7070ac90f2160e.png)
报错[DetectionNetwork(1)][warning]Network compiled for 6 shaves,maximum available 10,compiling for 5 s

机器学习笔记 - 卷积神经网络备忘清单

Install jupyter in the specified environment
随机推荐
OpenCV OAK相机对比及介绍
[software] ERP model selection method for large enterprises
Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC
Success and failure of ERP software from the perspective of enterprise evaluation
面試題 02.02. 返回倒數第 k 個節點
MSF给正常程序添加后门
2130. maximum twin sum of linked list
How to deal with these problems in the factory production process?
[share] how do enterprises carry out implementation planning?
[ERP system] how much do you know about the professional and technical evaluation?
When the enterprise makes a decision, which part should lead the ERP project?
Error [detectionnetwork (1)][warning]network compiled for 6 shapes, maximum available 10, compiling for 5 S
1400. construct K palindrome strings
ArcGIS 10.9.1 geological and meteorological volume metadata processing and service publishing and calling
openstack详解(二十二)——Neutron插件配置
【软件】ERP体系价值最大化的十点技巧
制作业信息化为什么难施行?
[237. delete nodes in the linked list]
Sword finger offer II 036 Postfix Expression
445. adding two numbers II