Sunday 20 December 2015

Python: multiprocessing: SimpleQueue: Exchange data between processes

It is minimal implementation of queue, used to exchange data between processes.

multiprocessing.SimpleQueue
Following are the methods provided by SimpleQueue.

Method
Description
empty()
Return True if the queue is empty, else False.
get()
Remove and return an item from queue
put(item)
Put an item into queue

from multiprocessing import Process, SimpleQueue


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

if __name__ == '__main__':
    queue = SimpleQueue()
    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("Is Queue empty", queue.empty())
    print(queue.get())
    print(queue.get())
    print("Is Queue empty", queue.empty())


Output
Is Queue empty False
[2, 3, 5, 7]
Hello
Is Queue empty True





Previous                                                 Next                                                 Home

No comments:

Post a Comment