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')