<p>评论建议您尝试在Windows上运行此操作。正如我在评论中所说</p>
<blockquote>
<p>If you're running this on Windows, it can't work - Windows doesn't
have <code>fork()</code>, so each process gets its own Queue and they have nothing
to do with each other. The entire module is imported "from scratch" by
each process on Windows. You'll need to create the Queue in <code>main()</code>,
and pass it as an argument to the worker function.</p>
</blockquote>
<p>这里详细说明了您需要做些什么才能使其可移植,尽管我删除了所有数据库内容,因为它与您目前描述的问题无关。我还去掉了摆弄,因为那通常是一种懒散的方式,避免把东西干干净净地关上,而且通常不会回来咬你:</p>
<pre><code>def process_append_queue(append_queue):
while True:
x = append_queue.get()
if x is None:
break
print("processed %d" % x)
print("worker done")
def main():
import multiprocessing as mp
append_queue = mp.Queue(10)
append_queue_process = mp.Process(target=process_append_queue, args=(append_queue,))
append_queue_process.start()
for i in range(100):
append_queue.put(i)
append_queue.put(None) # tell worker we're done
append_queue_process.join()
if __name__=="__main__":
main()
</code></pre>
<p>输出是“显而易见”的东西:</p>
<pre><code>processed 0
processed 1
processed 2
processed 3
processed 4
...
processed 96
processed 97
processed 98
processed 99
worker done
</code></pre>
<p>注意:由于Windows没有(不能)<code>fork()</code>,工作进程不可能继承Windows上的任何Python对象。每个进程从一开始就运行整个程序。这就是为什么原始程序不能工作:每个进程都创建了自己的<code>Queue</code>,与另一个进程中的<code>Queue</code>完全无关。在上面所示的方法中,只有主进程创建一个<code>Queue</code>,主进程将它(作为参数)传递给工作进程。</p>