Thread Communication

Let’s discuss thread communication using queues.

There are some use cases where you will want to have your threads communicate with each other. As we mentioned earlier, you can create an Event for this purpose. But a more common method is to use a Queue. For our example, we’ll actually use both!

Different methods with thread communication

Let’s see what that looks like:

import threading

from queue import Queue


def creator(data, q):
    """
    Creates data to be consumed and waits for the consumer
    to finish processing
    """
    print('Creating data and putting it on the queue')
    for item in data:
        evt = threading.Event()
        q.put((item, evt))

        print('Waiting for data to be doubled')
        evt.wait()


def my_consumer(q):
    """
    Consumes some data and works on it

    In this case, all it does is double the input
    """
    while True:
        data, evt = q.get()
        print('data found to be processed: {}'.format(data))
        processed = data * 2
        print(processed)
        evt.set()
        q.task_done()


if __name__ == '__main__':
    q = Queue()
    data = [5, 10, 13, -1]
    thread_one = threading.Thread(target=creator, args=(data, q))
    thread_two = threading.Thread(target=my_consumer, args=(q,))
    thread_one.start()
    thread_two.start()

    q.join()
Thread communication using queue.

Let’s break this down a bit. First off, we have a creator (AKA ...