我有一个最初在 Linux 上构建的 Python 程序,现在我正尝试将其移植到 Windows。我在包含所有依赖项的虚拟环境中运行该程序(我的程序使用 pip install --find-linkswheelsmy_module 作为轮子安装)。该计划启动于
(venv) C:\>venv\Scripts\python.exe -m base_module.Launcher arg1 arg2
base_module 按照提供的参数解释加载我的模块,他的相关代码是:
from multiprocessing.managers import SyncManager
import OtherCustomClass
class BaseModule(object):
def __init__(self, arg1, arg2):
self.manager = SyncManager()
self.manager.start(ignore_interrupt)
def main(argv=None):
ret = -1
try:
basmod = BaseModule(argv[0], argv[1])
ret = basmod.run()
except Exception, err:
print("error: " + str(err))
print(traceback.format_exc())
return ret
if __name__ == "__main__":
exitCode = main(sys.argv[1:])
sys.exit(exitCode)
这在 Linux 上运行良好,但在 Windows 上我得到以下异常:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python27\Lib\multiprocessing\forking.py", line 380, in main
prepare(preparation_data)
File "C:\Python27\Lib\multiprocessing\forking.py", line 505, in prepare
'__parents_main__', file, path_name, etc
File "build/bdist.linux-x86_64/egg/base_module/BaseModule.py", line 2, in <module>
ImportError: No module named OtherCustomClass
exception in main:
Traceback (most recent call last):
File "build/bdist.linux-x86_64/egg/base_module/BaseModule.py", line 12, in main
File "build/bdist.linux-x86_64/egg/base_module/BaseModule.py", line 7, in __init__
File "C:\Python27\Lib\multiprocessing\managers.py", line 528, in start
self._address = reader.recv()
EOFError
后一个 EOFError 是由 SyncManager 中的分叉意外提前终止引起的,其中真正的错误是无法导入 OtherCustomClass。我已经确认 OtherCustomClass 存在于 venv/lib/site-packages 中的 base_module 文件夹中,并且当我首先启动模块时不会发生此错误,因为 Python 永远不会到达 main() 或中的指令init如果脚本无法编译。
我做了一些研究,我知道这个问题已经困扰了其他人(通常使用第三方库,他们修复了问题而没有发布解决方案)。这似乎可以追溯到 Windows 缺少 fork(),以及 python 在 Windows 上对多处理的处理 - 另请参阅http://docs.python.org/library/multiprocessing.html#windows http://docs.python.org/library/multiprocessing.html#windows。但我不知道如何解决这个问题。
这是最新的 Python 2.7 分支 (2.7.8),在 Windows 7 x64 上运行。