Synchronous call process python3 pool

# coding:utf-8
import os
import time
from multiprocessing import Pool


def func(n):
    print("%s:%s" % (os.getpid(), n))
    return n**2

def func2(n):
    print("%s:%s" % (os.getpid(), n))
    time.sleep(1)
    return n**2


if __name__ == '__main__':
    start_time = time.time()
    the poolPool = (5)   # from scratch to create a process pool 5 process, after it has been in the process of five tasks 
    res_lst = []
     for i in the Range (10 ): 
        RES = pool.apply (FUNC, args = (i,))   # synchronous call, until this task is completed to get the res, wait for the process tasks func execution may or may not have a blocking blocking 
                                    # but exists regardless of whether the task is blocked, synchronous call will be in place waiting for 
        res_lst .append (RES)
     Print (res_lst)
     Print ( " execution time non-blocking process pool: " , time.time () - start_time) 

    S_TIME = time.time () 
    POOL2 = pool (5)   # process pool from none to there are five processes created after this has been in the process of five tasks 
    res_lst2 =[]
     For i in the Range (10 ): 
        RES = pool2.apply (func2, args = (i,))   # synchronous call, until this task is completed to get the res, wait for the process tasks performed func2 there may also be blocked may not block 
                                    # but exists regardless of whether the task is blocked, synchronous call will be in place waiting for 
        res_lst2.append (RES)
     Print (res_lst2)
     Print ( " have blocked the execution time of process pool: " , time.time () - S_TIME) 

# 9156: 0 
# 9156: 1 
# 9156: 2 
# 9156: 3 
# 9156: 4 
# 9156: 5 
# 9156: 6 
# 9156: 7 
# 9156: 8 
#9156: 9
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] 
# execution time blocking process pool: 0.515625 
# 5816: 0 
# 3324: 1 
# 3312: 2 
# 8228: 3 
# 9976 : 4 
# 5816: 5 
# 3324: 6 
# 3312: 7 
# 8228: 8 
# 9976: 9 
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] 
# obstructions process pool execution time: 10.421875

 

Guess you like

Origin www.cnblogs.com/lilyxiaoyy/p/10986600.html