当前位置:网站首页>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 
"""

原网站

版权声明
本文为[lwj_ 07]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020507217105.html