我正在产生一个函数的几个线程并收集它的输出。这是代码。
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
上面的代码有效,我可以收集所需的输出。但是,当我切换到产生Process
(取消注释Process
的行并相应地注释Thread
行时)q.get
然后锁定应用程序,这意味着{{1} object没有任何返回的结果,所以它无限地等待。