Python: multiprocessing.Process

 

 

import threading, time, logging, random, datetime, multiprocessing

FORMAT = '%(asctime)-15s %(process)d %(lineno)-3s [%(threadName)-11s %(thread)6d] %(message)s'
logging.basicConfig(format=FORMAT, level=logging.DEBUG)


def calc(i):
    sum = 0
    for _ in range(1000000000):
        sum += 1


if __name__ == '__main__':
    commence = datetime.datetime.now()

    vails = []

    for i in range(5):
        p = multiprocessing.Process(name=f'Process-{i}', target=calc, args=(i,))
        p.start()
        vails.append(p)

    for p in vails:
        p.join()

    delta = (datetime.datetime.now() - commence).total_seconds()
    logging.error(delta)

cpu利用率占满

 

 

 

 

 

 

使用multiprocessing.Pool

import datetime, multiprocessing, logging

FORMAT = '%(asctime)-15s [%(processName)-18s %(process)-6d] %(lineno)-3s [%(threadName)-11s %(thread)6d] %(message)s'
logging.basicConfig(format=FORMAT, level=logging.DEBUG)


def calc(i):
    sum = 0
    for _ in range(100000000):
        sum += 1
    logging.critical(f'i: {i}, sum: {sum}')


if __name__ == '__main__':
    commence = datetime.datetime.now()
    pool = multiprocessing.Pool(5)
    for i in range(5):
        pool.apply_async(func=calc, args=(i,))
    pool.close()  # must close before join
    pool.join()
    delta = (datetime.datetime.now() - commence).total_seconds()
    logging.error(delta)

 

posted @ 2022-02-25 22:37  ascertain  阅读(62)  评论(0编辑  收藏  举报