python学习笔记08 Process

版权声明: https://blog.csdn.net/qq_40025335/article/details/81607506
#多进程间通信
from multiprocessing import Process, Queue
import time
import  os

def write(q):
    print("启动写子进程%s" %(os.getpid()))
    for chr in ["A","B", "C", "D"]:
        q.put(chr)
        time.sleep(1)

    print("结束写子进程%s"%(os.getpid()))



def read(q):
    print("启动读子进程%s" %(os.getpid()))
    while True:
    # for 1 in 4
        value = q.get(True)
        print("value = " +value)
    print("结束子进程%s" %(os.getpid()))


if __name__ == "__main__":
    #父进程创建队列。,并传递给子进程
    q = Queue()
    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))

    pw.start()
    pr.start()

    #
    pw.join()
    #pr进程里是个死循环,无法等待期结束,只能强行结束
    pr.terminate()
-------------------------

G:\Python\testproject1\venv1\Scripts\python.exe G:/Python/testproject1/venv1/pyMutilProcess/进程间通信.py
启动写子进程16284
启动读子进程11716
value = A
value = B
value = C
value = D
结束写子进程16284

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/qq_40025335/article/details/81607506
今日推荐