user554481
user554481

Reputation: 2109

python thread stops writing to output queue

I have a thread that reads data from an input queue, does something with it, and then puts the result on an output queue to be consumed later. However, it looks like the thread is only able to write to the output queue once. What am I doing wrong? Below is a highly simplified, self-contained version of my code.

import queue
import threading


def do_work(input_queue,output_queue):
    input_number = input_queue.get()
    output_number = input_number ** 2
    input_queue.task_done()
    output_queue.put(output_number)

input_queue = queue.LifoQueue()
output_queue = queue.LifoQueue()

t = threading.Thread(name="Some thread", target=do_work, args=(input_queue,output_queue))
t.start()
for i in range(1000):
    input_queue.put(i)
    if output_queue.empty():
        print('input: {0}'.format(i))
    else:
        result = output_queue.get()
        print('result: {0}'.format(result))
        output_queue.task_done()

This results in the following output:

input: 0
result: 0
input: 2
input: 3
input: 4
...
input: 999

The "result" is only printed once, but I expected it to be printed multiple times, once for each input.

Upvotes: 0

Views: 535

Answers (1)

eozd
eozd

Reputation: 1193

When you call start on your thread, it starts executing the function you passed to it. In this case, you call the get method of your queue which blocks your thread until your input queue has at least one element. As soon as your input queue has one element, your function continues executing and puts your first input into the output queue. However, note that once this operation is done a single time, your function returns and thus your thread exits.

To make your thread repeatedly query your input queue for an input, you should put a while to wrap your function to the beginning. For example

while True:
    input_number = input_queue.get()
    output_number = input_number ** 2   
    input_queue.task_done()
    output_queue.put(output_number)

In this case, after a single element is queried, processed and put to the output queue, your worker thread will wait for a new input element to appear in your input queue. Once there is an element, it will perform the same processing steps again.

Upvotes: 2

Related Questions