Use Queue() to communicate between processes

 
 

Communication between multiple processes can be done using Queue() in multiprocessing. Let’s briefly introduce the functions and functions in Queue().

When initializing the Queue() object (for example: q=Queue()), if the maximum number of messages that can be received is not specified in the parentheses, or the number is negative, then there is no upper limit on the number of acceptable messages (until the end of memory) ;

  • Queue.qsize(): Returns the number of messages contained in the current queue;

  • Queue.empty(): If the queue is empty, return True, otherwise False;

  • Queue.full(): If the queue is full, return True, otherwise False;

  • Queue.get([block[, timeout]]): Get a message in the queue, and then remove it from the queue, the default value of block is True;

1) If the default value of block is used and the timeout (in seconds) is not set, if the message queue is empty, the program will be blocked (stop in the reading state) until the message is read from the message queue, if the timeout is set , it will wait for timeout seconds, and if no message has been read, a "Queue.Empty" exception will be thrown;

2) If the block value is False, if the message queue is empty, the "Queue.Empty" exception will be thrown immediately;

  • Queue.get_nowait():相当Queue.get(False);

  • Queue.put(item,[block[, timeout]]): Write the item message to the queue, the default value of block is True;

1) If the default value of block is used and the timeout (in seconds) is not set, if the message queue has no space to write, the program will be blocked (stop in the writing state) until space is vacated from the message queue. If timeout is set, it will wait for timeout seconds, and if there is no space, a "Queue.Full" exception will be thrown;

2) If the block value is False, if there is no space to write in the message queue, the "Queue.Full" exception will be thrown immediately;

  • Queue.put_nowait(item):相当Queue.put(item, False);

See a simple example below:

The process p_get executes the function get_data to obtain data from the queue q, and the process p_put executes the function put_data to write data to the queue q.

from multiprocessing import Queue, Process

import time


def get_data(q):
    """Get data from queue q"""
    while True:
        if q.qsize:
            print('Remove %s' % q.get())
        time.sleep(0.6)


def put_data(q):
    """Add data to queue q"""
    count = 0
    while True:
        count += 1
        if q.qsize() < 100:
            q.put("%s data" % count)
            print("Put the %s data" % count)
        time.sleep(0.5)


def main():
    q = Queue()
    p_get = Process(target=get_data, args=(q,))
    p_get.start()
    p_put = Process(target=put_data, args=(q,))
    p_put.start()
    p_put.join()
    p_get.join()


if __name__ == '__main__':
    main()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325707686&siteId=291194637