欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python编程:multiprocessing多进程(代码实例)

程序员文章站 2022-08-11 12:36:51
获取进程id import multiprocessing import os def foo(): print(__name__) print(...

获取进程id

import multiprocessing
import os

def foo():
    print(__name__)
    print("parent:", os.getppid())
    print("current:", os.getpid())

if __name__ == "__main__":
    foo()  # 主进程调用

    p = multiprocessing.Process(target=foo)
    p.start()  # 新开进程调用

    """
    __main__
    parent: 952
    current: 188
    __mp_main__
    parent: 188
    current: 6492
    """

多进程测试

import multiprocessing
import time, threading
from pycharm_fontcolor import Colors, FontColor

def func(i):
    print(FontColor.set_color("线程号:{}".format(threading.get_ident()), Colors.green), i)

def foo(i):
    print(FontColor.set_color("进程:", Colors.red), i)
    t = threading.Thread(target=func, args=(i,))  # 进程中新开线程
    t.start()

if __name__ == "__main__":
    start_time = time.time()

    for i in range(5):
        p = multiprocessing.Process(target=foo, args=(i,))
        p.start()

    end_time = time.time()

    print(FontColor.set_color("时间:", Colors.blue), end_time - start_time)

    """
    时间: 0.05303597450256348
    进程: 0
    线程号:8968 0
    进程: 1
    线程号:7620 1
    进程: 2
    进程: 3
    线程号:9204 2
    线程号:2492 3
    进程: 4
    线程号:6512 4
    """

进程池

from multiprocessing import  Pool
import time, os

def foo(i):
    time.sleep(2)
    print("process:", i, os.getpid())
    return i + 1  # 参数将传递给回调函数

def bar(i): # 做回调函数,由主进程执行
    print("bar", i, os.getpid())


if __name__ == "__main__":
    print(os.getpid())
    pool = Pool(processes=3)  # 允许同时放入3个进程
    for i in range(10):
        # p = pool.apply(func=foo, args=(i,))  # 串行
        p = pool.apply_async(func=foo, args=(i,), callback=bar)  # 并行

    print("done")
    pool.close()  # 先关闭入口
    pool.join()  # 在等待执行
    """
    8120
    done
    process: 0 5452
    bar 1 8120
    process: 1 8360
    bar 2 8120
    process: 2 7908
    bar 3 8120
    process: 3 5452
    bar 4 8120
    process: 4 8360
    bar 5 8120
    process: 5 7908
    bar 6 8120
    process: 6 5452
    bar 7 8120
    process: 7 8360
    bar 8 8120
    process: 8 7908
    bar 9 8120
    process: 9 5452
    bar 10 8120
    """

进程队列

from multiprocessing import Process, Queue

def foo(q):
    q.put("你好")

if __name__ == "__main__":
    q = Queue()  # 进程队列
    p = Process(target=foo, args=(q,))
    # p = Thread(target=foo, args=(q,))
    p.start()

    print(q.get())  # 你好

进程管道

from multiprocessing import Process, Pipe

def foo(conn):
    conn.send("hello")
    conn.close()

if __name__ == "__main__":
    parent_conn, child_conn = Pipe()  # 管道传送数据

    p = Process(target=foo, args=(child_conn,))
    p.start()

    print(parent_conn.recv())  # hello

多进程实现数据共享

from multiprocessing import Process, Manager
import os

def foo(dct, lst):
    dct[os.getpid()] = os.getpid()
    lst.append(os.getpid())
    print(os.getpid())

if __name__ == "__main__":
    manager = Manager()  # 实现进程间共享数据
    dct = manager.dict()
    lst = manager.list()
    processes = []
    for i in range(5):
        p = Process(target=foo, args=(dct, lst))
        p.start()
        processes.append(p)

    for process in processes:  # 等待结果
        process.join()

    print(dct)
    print(lst)

    """
    3816 5376 7500 8332 1124
    {3816: 3816, 5376: 5376, 8332: 8332, 7500: 7500, 1124: 1124}
    [3816, 5376, 7500, 8332, 1124]
    """

进程锁

from multiprocessing import Lock, Process

def foo(lock, i):
    lock.acquire()
    print("hello world!", i)
    lock.release()

if __name__ == "__main__":  # 主动执行会继续下面的内容,被当做模块调用则不会
    lock = Lock()  # 实例化进程锁
    for i in range(5):
        p = Process(target=foo, args=(lock, i))
        p.start()

    """
    hello world! 2
    hello world! 1
    hello world! 0
    hello world! 3
    hello world! 4
    """