Python multiprocessing Pool map和 map_async实例

# coding=utf-8
import logging
from multiprocessing import Pool

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [*] %(processName)s %(message)s"
)

import time


def add_test (i):
    time.sleep(1)
    return i * i


def add (x, y):
    time.sleep(1)
    return x + y


def add_wrap (args):
    return add(*args)


if __name__ == "__main__":
    start = time.time()
    logging.info("-----main before")
    pool = Pool()
    # pool.map(add_test, [i for i in range(16)])  # 五个进程:4.254904508590698 s 一个进程:4.352669715881348 s
    pool.map_async(add_test, [i for i in range(16)])  # 五个进程:4.248882055282593 s 一个进程:4.340830564498901 s
    # 通过一个序列的方式来实现函数之间的映射, 并且 并行执行
    # result = pool.map(add_wrap, [  # 五个进程:3.352376937866211 s 一个进程:4.250034809112549 s
    #     (1, 2), (3, 4), (5, 6),
    #     (1, 2), (3, 4), (5, 6),
    #     (1, 2), (3, 4), (5, 6),
    #     (1, 2), (3, 4), (5, 6),
    #     (1, 2), (3, 4), (5, 6)],
    #                   )
    # 适用于并发
    # result = pool.map_async(add_wrap, [  # 五个进程:3.3456337451934814 s 一个进程:4.341647624969482 s
    #     (1, 2), (3, 4), (5, 6),
    #     (1, 2), (3, 4), (5, 6),
    #     (1, 2), (3, 4), (5, 6),
    #     (1, 2), (3, 4), (5, 6),
    #     (1, 2), (3, 4), (5, 6)],
    #                         )
    pool.close()
    pool.join()
    logging.info(f"-----main after {time.time()-start} s")

你可能感兴趣的:(Python multiprocessing Pool map和 map_async实例)