python 多进程实例

import datetime
import time
import sys
import multiprocessing

TEST = {}
TEST['test1'] = 1
TEST['test2'] = 12
TEST['test3'] = 13
TEST['test4'] = 14
TEST['test5'] = 15
TEST['test6'] = 16
TEST['test7'] = 17
TEST['test8'] = 18
def test_list(test):
    print(test)
    time.sleep(10)

if __name__== '__main__':
    ps = []
    for test in GGI:
        p = multiprocessing.Process(target=test_list, args=(test,))
        p.start()
        ps.append(p)
      
    for process in ps:
        process.join()
    print('ok')
 
 
 
 


打印每个子进程信息。

if __name__== '__main__':
    ps = []
    for test in GGI:
        p = multiprocessing.Process(target=test_list, args=(test,))
        p.start()
        ps.append(p)
    print('ok')
    print("The number of CPU is:" + str(multiprocessing.cpu_count()))
    for p in multiprocessing.active_children():
        print("child   p.name:" + p.name + "\tp.id:" + str(p.pid))
    print("END!!!!!!!!!!!!!!!!!")
    for process in ps:
        process.join()

使用进程池,控制启用进程数量。

if __name__== '__main__':
    ps = []
    pool = multiprocessing.Pool(processes = 3)
    for test in TEST:
        pool.apply_async(test_list, (test, ))
    print('ok')
    pool.close()
    pool.join()
    print('end')

也可以根据CUP核数,启用进程数量。

if __name__== '__main__':
    ps = []
    cpu_count = multiprocessing.cpu_count()
    pool = multiprocessing.Pool(processes = cpu_count)
    for test in TEST:
        pool.apply_async(test_list, (test, ))
    print('ok')
    pool.close()
    pool.join()
    print('end')


你可能感兴趣的