Process.start在Windows上无法使用
我在一个运行着Linux和Windows的电脑上使用Python 2.7.2。现在我有一个脚本,它通过multiprocessing.Process
来生成子进程,然后等待这些子进程完成任务,最后退出。这个在Linux上运行得很好,但在Windows上执行process.start
时却出现了错误。
下面是一个示例代码片段:
pobj = Process(target = foo, args=(bar,))
pobj.start()
在Windows系统上的错误:
WindowsError: [Error 2] The system cannot find the file specified
我做了一些基本的调试,发现错误出现在“C:\Python27\Lib\multiprocessing\forking.py”文件的第255行。
1 个回答
3
因为Windows系统没有fork()
这个系统调用,所以multiprocessing
模块需要用一些特殊的方法来分叉进程。简单来说,它会尝试猜测你程序的入口点,并跳过在分叉之前执行的所有代码。
可能你在代码中没有加入一个
if __name__ == '__main__':
保护措施,这对于在Windows上正常运行代码是必要的——想了解更多信息,可以查看这个平台特定的文档。