【发布时间】:2018-09-24 15:39:13
【问题描述】:
问题
我正在尝试让setup.py test 使用在 Windows 上使用多处理的包。我有以下情况:
- 一个常规的python包文件夹结构
- 有一个包
- 一个测试文件夹和
-
setup.py。
跑步
python -m unittest
从顶层目录执行我的测试,没有任何抱怨。但是,调用时
python setup.py test
我收到了众所周知的freeze_support 问题(请参阅此处关于 SO 的几个问题):
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
但是,以下 where to put freeze_support() in a Python script? 实际上,我的所有文件中都有所描述的保护措施,可能太多了。
问题 1:我必须在我的项目中进行哪些更改才能使 setup.py test 使用我的多处理使用函数?
问题 2: 我放置的 freeze_support() 中的哪一个是真正需要的?据我所知,这通常只在编译冻结的可执行文件时才需要。
MWE
文件夹结构
project
│ setup.py
│
└─── my_package
│ │ __init__.py
│ │ my_package.py
│
└─── test
│ │ __init__.py
│ │ test_it.py
__init__.py 文件为空,其他为:
my_package.py
import multiprocessing
def say_hi(arg):
print('hi')
def main():
pool = multiprocessing.Pool(1)
pool.map(say_hi, range(1))
if __name__ == '__main__':
multiprocessing.freeze_support()
main()
test_it.py
import multiprocessing
import unittest
from my_package import my_package
class TestIt(unittest.TestCase):
def test_say_hi(self):
my_package.main()
if __name__ == '__main__':
multiprocessing.freeze_support()
setup.py
#!/usr/bin/env python
import setuptools
import multiprocessing
setuptools.setup(
name = "my_package"
)
if __name__ == '__main__':
multiprocessing.freeze_support()
【问题讨论】:
标签: python windows multiprocessing setuptools