0

我有一个在这里定义的超级简单的python脚本

import multiprocessing
from multiprocessing import Pool

print "CPUs: " + str(multiprocessing.cpu_count())
convertPool = Pool(multiprocessing.cpu_count())

在 Linux 上,这似乎与我预期的一样,程序刚刚启动,然后打印出内核数,然后退出。但是,在 Windows 上,该程序将继续调用新的 python 命令并打印出“CPUs: 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 和 bar 只是占位符。在 Linux 上,这将启动 multiprocessing.cpu_count() 数量的 ConvertImage.exe 进程,然后在所有 ConvertImage.exe 进程完成后打印 Finished processing。在 Windows 上,这会立即启动 len(bar) ConvertImage.exe 进程,然后立即打印 Finished processing 并退出。如何让 Windows 版本表现得像 Linux 版本?

4

1 回答 1

0

我弄清楚了,它工作正常,我的示例程序发布错误,因为我忘记了一些导入并导致了所描述的行为。在我得到正确的所有导入之后,这个示例在 Windows 和 Linux 上运行良好。

于 2012-07-16T18:07:28.653 回答