使用multithreading.Process时,Python Queue.get()会冻结执行吗?

时间:2015-03-13 19:45:38

标签: python multithreading queue multiprocessing

我正在产生一个函数的几个线程并收集它的输出。这是代码。

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没有任何返回的结果,所以它无限地等待。

  1. 为什么会这样?

0 个答案:

没有答案