Python 多进程编程

import multiprocessing
import time
import os
import random
g_nums = [11, 22, 33]


def test1():
    while True:
        print("子进程PID = %d,父进程PID = %d" % (os.getpid(), os.getppid()))
        time.sleep(1)


def test2(a, b, c, *args, **kwargs):
    print(a)
    print(b)
    print(c)
    print(args)
    print(kwargs)


def test3():
    global g_nums
    g_nums.append(44)
    print("test3:", end="")
    print(g_nums)


def test4():
    global g_nums
    print("test4:", end="")
    print(g_nums)


def download_from_web(q):
    """ 下载数据 """
    # 模拟从网上下载数据
    data = [11, 22, 33, 44]
    # 向队列中写入数据
    for temp in data:
        q.put(temp)
    print("下载器已经下载完了数据,并存入到了队列中!")


def analysis_data(q):
    """ 数据处理 """
    wait_analysis_data = list()
    while True:
        data = q.get()
        wait_analysis_data.append(data)
        if q.empty():
            break
    print("模拟数据处理:" + str(wait_analysis_data))


def work(msg):
    t_start = time.time()
    print("%d开始执行,进程PID:%d" % (msg, os.getppid()))
    # random.random()随机生成0~1之间的浮点数
    time.sleep(random.random() * 2)
    t_stop = time.time()
    print("%d执行完毕,耗时%0.2f" % (msg, t_stop - t_start))


def main():
    print("主进程PID = %d,父进程PID = %d" % (os.getpid(), os.getppid()))
    # 创建一个队列
    q = multiprocessing.Queue()
    # 定义一个进程池,最大进程数3
    po = multiprocessing.Pool(3)
    p1 = multiprocessing.Process(target=test1)
    p2 = multiprocessing.Process(target=test2, args=(11, 22, 33, 44, 55, 66, 77), kwargs={"籍贯": "常山", "姓名": "赵子龙"})
    p3 = multiprocessing.Process(target=test3)
    p4 = multiprocessing.Process(target=test4)
    # 创建多个进程,将队列的引用当做实参传递到里面
    p5 = multiprocessing.Process(target=download_from_web, args=(q,))
    p6 = multiprocessing.Process(target=analysis_data, args=(q,))
    # p1.start()
    # p2.start()
    # # 进程3 和 进程4 说明:多进程之间不共享全局变量
    # p3.start()
    # p4.start()
    # # 进程5 和 进程6 演示了多进程之间通过Queue 来实现数据共享
    # p5.start()
    # p6.start()
    for i in range(1, 11):
        # Pool().apply_async(要调用的目标,(传递给目标的参数元祖,))
        # 每次循环将会用空闲出来的子进程去调用目标
        po.apply_async(work, args=(i,))
    print("------start------")
    # 关闭进程池,关闭后po不再接收新的请求
    po.close()
    # 等待po中所有子进程执行完成,必须放在close语句之后
    po.join()
    print("------end------")


if __name__ == '__main__':
    main()

 

posted @ 2019-08-11 20:31  李道臣  阅读(193)  评论(0编辑  收藏  举报