Sunday, 20 December 2015

Python: multiprocessing: Queue: Exchange data between processes

Queue is a simple data structure used for data exchange between multiple processes. Queue class is internally synchronized.

multiprocessing.Queue([maxsize])
Multiprocessing module provides Queue class to define a queue.

Following are the methods provided by Queue class.


Method
Description
qsize()
Returns approximate size of the queue
empty()
Return true if the queue is empty, else false
full()
Return true, if the queue is full, else false.
put(obj[, block[, timeout]])
Put an object into queue. Arguments block, timeout are optional. If block is set to True, then the process blocks until free slot available to put an item into queue.

If block is false, then puts an item into queue, if the slot available, else queue.Full exception is thrown.

If timeout > 0, then it blocks at most timeout seconds and raises the queue.Full exception if no free slot was available within that time.
put_nowait(obj)
Equivalent to put(obj, False).
get([block[, timeout]])
Remove and return an item from queue. Arguments block, timeout are optional.

If block is True, then process will wait until the queue has some item.

If block is False and queue is not empty, it return an item, if queue is empty raise the queue.Empty exception

If timeout is > 0, it blocks at most timeout seconds and raises the queue.Empty exception if no item was available within that time.
get_nowait()
Equivalent to get(False).
close()
Used to close the queue. After calling this method, no more data will be put on this queue by the current process.  This method is called automatically, when the queue is garbage collected.
join_thread()
This method is called after calling close method. It ensures all the data in the buffer is flushed and blocks until the background thread exits.
cancel_join_thread()
It prevents background thread from blocking.

from multiprocessing import Process, Queue


def add_to_queue(queue, data):
    queue.put(data)

if __name__ == '__main__':
    queue = Queue(10)
    process1 = Process(target=add_to_queue, args=(queue,[2, 3, 5, 7]))
    process2 = Process(target=add_to_queue, args=(queue,"Hello"))

    process1.start()
    process2.start()

    process1.join()
    process2.join()

    print("Maximum size of Queue", queue._maxsize)
    print("Is Queue empty", queue.empty())
    print(queue.get())
    print(queue.get())
    print("Is Queue empty", queue.empty())


Output
Maximum size of Queue 10
Is Queue empty False
[2, 3, 5, 7]
Hello
Is Queue empty True









Previous                                                 Next                                                 Home

No comments:

Post a Comment