Python inter-process communication Queue instance parsing, pythonqueue
This article mainly studies the Python process-to-process communication Queue examples, as detailed below.
1. Queue usage:
- Queue. qsize (): returns the number of messages contained in the current Queue;
- Queue. empty (): If the Queue is empty, True is returned; otherwise, False is returned;
- Queue. full (): If the Queue is full, True is returned; otherwise, False is returned;
- Queue. get (): gets a message in the Queue and removes it from the Queue. The parameter timeout period can be passed.
- Queue. get_nowait (): equivalent to Queue. get (False). If the value is not obtained, an exception is triggered: Empty;
- Queue. put (): adds a value to the sequence to pass the parameter timeout period.
- Queue. put_nowait (): equivalent to Queue. get (False). When the Queue is Full, an error is returned: Full.
2. Queue instance:
Come, the code above:
#! /Usr/bin/env python3import timefrom multiprocessing import Process, Queueq = Queue () # create a Queue. If no number is passed, there is no limit to the number of queues for I in range (11): q. put (I) def A (): while 1: try: num = q. get_nowait () print ('process A, retrieve the number: % d' % num) time. sleep (1) failed T: breakdef B (): while 1: try: num = q. get_nowait () print ('process B, retrieve the number: % d' % num) time. sleep (1) failed T: breakp1 = Process (target = A) p2 = Process (target = B) p1.start () p2.start ()
This program adds 10 numbers to the queue and then uses two processes to retrieve them.
Running result:
I am process A, and the number is 0.
I am process B and retrieve the number: 1
I am process A and retrieve the number: 2
I am process B, and the number is 3.
I am process A and retrieve the number: 4
I am process B, and the number is 5.
I am process B, and the number is 6.
I am process A and retrieve the number: 7
I am process B, and the number is 8.
I am process A and retrieve the number: 9
I am process B, and the number is 10.
3. When using the process Pool, the Queue will encounter an error. You need to use Manager. Queue:
Code on
#! /Usr/bin/env python3import timefrom multiprocessing import Pool, Manager, Queueq = Manager (). queue () for I in range (11): q. put (I) def A (I): num = q. get_nowait () print ('I am a process % d, retrieve the number: % d' % (I, num) time. sleep (1) pool = Pool (3) for I in range (10): pool. apply_async (A, (I,) pool. close () pool. join ()
Running result:
I am process 1 and retrieve the number: 0
I am a process 0 and retrieve the number: 1
I am process 2 and retrieve the number: 2
I am process 4 and retrieve the number: 3
I am process 3 and retrieve the number: 4
I am process 5, and retrieve the number: 5
I am process 6, and the number is 6.
I am a process 7 and retrieve the number: 7
I am PROCESS 8 and retrieve the number: 8
I am process 9 and retrieve the number: 9
If you change Manager (). Queue () to Queue () directly, there may be resource confusion and a lack of processes.
4. The main Process defines a variable of the Queue type and transmits it as the args parameter of the Process to the sub-Process processA and processB. The two processes write data to the Queue and read data.
import timefrom multiprocessing import Process,QueueMSG_QUEUE = Queue(5)def startA(msgQueue): while True: if msgQueue.empty() > 0: print 'queue is empty %d' % (msgQueue.qsize()) else: msg = msgQueue.get() print 'get msg %s' % (msg,) time.sleep(1)def startB(msgQueue): while True: msgQueue.put('hello world') print 'put hello world queue size is %d' % (msgQueue.qsize(),) time.sleep(3)if __name__ == '__main__': processA = Process(target=startA,args=(MSG_QUEUE,)) processB = Process(target=startB,args=(MSG_QUEUE,)) processA.start() print 'processA start..' processB.start() print 'processB start..'
The printed result is as follows:
C: \ Python27 \ python.exe E:/outofmemory/test/queuetest. py
ProcessA start ..
ProcessB start ..
Queue is empty 0
Put hello world queue size is 1
Get msg hello world
Queue is empty 0
Queue is empty 0
Put hello world queue size is 1
Get msg hello world
Queue is empty 0
Queue is empty 0
Put hello world queue size is 1
Summary
The above is all the content of this article on the parsing of the Python inter-process communication Queue instance, I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!