I am spawning several threads of one function and collect its output. Here is the code.
from threading import Thread
from multiprocessing import Process, Queue
def myfunc():
def count_freqs(part, q):
freqs = (do some stuff)
q.put(freqs)
def thread_joiner(data_parts):
threads = []
q = Queue()
for part in data_parts:
thread = Thread(target=count_freqs, args=(part, q))
#thread = Process(target=count_freqs, args=(part, q))
thread.start()
threads.append(thread)
for t in threads:
t.join()
return [q.get() for _ in range(len(data_parts))]
new_freqs = thread_joiner(data_parts)
return new_freqs
The code above works and I am able to collect the required output. However when I switch to spawning Process
(uncomment the line with Process
and comment the line with Thread
accordingly) the q.get
then locks the application which means that Queue
object does not have any results to return so it infinitely waits.
- Why is that happening?