奇怪我有一個超級簡單的Python腳本如下的Python multiprocessing.Pool是在Windows
import multiprocessing
from multiprocessing import Pool
print "CPUs: " + str(multiprocessing.cpu_count())
convertPool = Pool(multiprocessing.cpu_count())
在Linux定義這似乎表現爲我的預計,隨着該計劃剛剛起步,然後打印出核心數量,然後退出。然而,在Windows上,這個程序將繼續調用新的python命令並打印出「CPU:4」(我有4個內核)並且永不停止運行,並且最終會終止該命令。有人能解釋一下這裏發生了什麼嗎?
感謝
編輯: 我現在有下面的程序仍然不工作,我期望
import sys
import os
import subprocess
from subprocess import Popen, PIPE
from threading import Thread
import multiprocessing
from multiprocessing import Pool, freeze_support
try:
from Queue import Queue, Empty
except ImportError:
from queue import Queue, Empty # python 3.x
ON_POSIX = "posix" in sys.builtin_module_names
def myPrint(line, logWriter):
if logWriter is None:
# This will print without a newline, cause the process
# has its own newlines
sys.stdout.write(line)
else:
logWriter.write(line.strip())
logWriter.write("\n")
logWriter.flush()
# This is gotten from http://stackoverflow.com/questions/375427/non-blocking-read-on-a-subprocess-pipe-in-python
def executeCommand(cmd, logWriter):
myPrint(cmd + "\n", logWriter)
p = Popen(cmd, stdout=PIPE, bufsize=4096, close_fds=ON_POSIX, shell=True)
q = Queue()
t = Thread(target=enqueue_output, args=(p.stdout, q))
t.daemon = True # thread dies with the program
t.start()
# read line without blocking
while t.isAlive() or not q.empty():
try:
line = q.get_nowait() # or q.get(timeout=.1)
except Empty:
pass # Do nothing
else: # If there is a line, then print it
if logWriter is not None:
myPrint(line, logWriter)
# Sleep to prevent python from using too much cpu
time.sleep(0.05)
if __name__ == "__main__":
freeze_support()
convertPool = Pool(multiprocessing.cpu_count())
# Now let's smooth and threshold all the masks
for foo in bar:
threshSmoothCmd = "ConvertImage.exe -i " + foo + " -o " + foo
myPrint(threshSmoothCmd + "\n", None)
convertPool.apply_async(executeCommand,(threshSmoothCmd, None,))
convertPool.close()
convertPool.join()
print "Finished processing"
ConvertImage.exe是一個可執行文件我寫我自己foo和酒吧都只是佔位符。在Linux上,這將啓動Multiprocessing.cpu_count()數量的ConvertImage.exe進程,並在所有ConvertImage.exe進程完成後打印完成的進程。在Windows上,立即啓動len(bar)ConvertImage.exe進程,然後立即打印完成的處理並退出。我如何讓Windows版本像Linux版本一樣運行?
包括'if __name__ =='__main __「:'使這個工作。這在'multiprocessing'文檔中有介紹。 – 2012-07-16 17:09:52
另外請注意,在我看來,它不是'multiprocessing.Pool',這很奇怪。奇怪的是Windows缺少'fork()'。 – 2012-07-16 17:10:38
[python3.x multiprocessing cycling without possible if if __name__ =='__main__':「]的可能重複(http://stackoverflow.com/questions/11501048/python3-x-multiprocessing-cycling-without-if-name-main ) – 2012-07-16 17:13:25