Communication between processes 32

Inter-Process Communication (Inter-Porcess Communication, referred to as IPC), we must first understand the queue, it is FIFO. By from multiprocessing import Queueuse.

from multiprocessing import Queue
q = Queue(6)
for i in range(6):
    q.put(i)
    print('%d放入队列'%i)
print('********************************')
while True:
    print(q.get())

Through the queue, so that communication between the two sub-processes.

from multiprocessing import Queue,Process
def produce(q):
    q.put('hello')
def consume(q):
    print(q.get())
if __name__ == '__main__':
    q = Queue()
    p = Process(target=produce,args=(q,))
    p.start()
    p1 = Process(target=consume,args=(q,))
    p1.start()

Producers, consumers model

A queue is a safe process, the data can only be a queue a data request process.

from multiprocessing import Queue,Process
import time,random
def producer(name,food,q):
    for i in range(10):
        time.sleep(random.randint(1,3))
        f = '%s生产了%s%s'%(name,food,i)
        print(f)
        q.put(f)

def consumer(q,name):
    while True:
        food = q.get()
        if food is None:
            print('获取到一个空')
            break
        print('%s消费了%s'%(name,food))
        time.sleep(random.randint(1,3))

if __name__ == '__main__':
    q = Queue()
    p1 = Process(target=producer,args=('张三','包子',q))
    p2 = Process(target=producer,args=('李四','胡辣汤',q))
    c1 = Process(target=consumer,args=(q,'王五'))
    c2 = Process(target=consumer,args=(q,'yan五'))
    p1.start()
    p2.start()
    c1.start()
    c2.start()
    p1.join()
    p2.join()
    q.put(None)
    q.put(None)

JoinableQueue

from multiprocessing import JoinableQueue,Process
def producer(name,food,q):
    for i in range(10):
        time.sleep(random.randint(1,3))
        f = '%s生产了%s%s'%(name,food,i)
        print(f)
        q.put(f)
    q.join()    #JoinableQueue的join代表阻塞 感知一个队列中的数据全部被执行完毕。

def consumer(q,name):
    while True:
        food = q.get()
        print('%s消费了%s'%(name,food))
        time.sleep(random.randint(1,3))
        q.task_done()   #提交一个回值,对q中的值的个数减一。
if __name__ == '__main__':
    q = JoinableQueue()
    p1 = Process(target=producer,args=('张三','包子',q))
    p2 = Process(target=producer,args=('李四','胡辣汤',q))
    c1 = Process(target=consumer,args=(q,'王五'))
    c2 = Process(target=consumer,args=(q,'yan五'))
    p1.start()
    p2.start()
    c1.daemon = True    #设置为守护进程,主进程代码执行完毕之后,子进程自动结束。
    c2.daemon = True
    c1.start()
    c2.start()
    p1.join()
    p2.join()   #进程的join是感知一个进程的结束

In the consumer side:

  • Each time a data acquisition, sending a token, namely: q.task_done(), a flag data is processed successfully.

The producer side:

  • Each producing a data put into the queue, and the data to be a data producer when finished all the production data, jointhe signal has stopped production data, have to wait for data to be consumed when the data has been processed, jointhe end of the obstruction .

  • consumer in all the tasks consumed

  • producer of joinperceived blocking stop

  • End all producer processes

  • Primary process p.joinends

  • Primary process end code

  • Daemon (consumer process) ends

Guess you like

Origin blog.csdn.net/weixin_43265998/article/details/90523125