I'm developing a python package, in which multiprocessing package will be used. However, when run it on windows machines, I met a problem. The demo code is shown as follows.
structure of the demo
|pkg
| __init__.py
| pkg_script.py
|test.py
pkg is the package folder, test.py is a python script that uses the package.
init.py
from .pkg_script import *
pkg_script.py
import multiprocessing as mp
def task(task_id):
print(f'task {task_id}')
def func():
p = mp.Pool(4)
p.map(task,range(4))
test.py
import pkg
pkg.func()
The error message I got:
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.
The reason is that, on windows machines, each sub processes will run scripts from the beginning. Therefore, pkg.func() in the test.py will also be conducted in each sub processes. To avoid this issue, we can put pkg.func() in the test.py under if __name__ == '__main__'. However, as I'm developing a package, I cannot enforce users to do so. Users may put pkg.func() anywhere in their codes. Is there any solutions for that without using if __name__ == '__main__'?
EDIT1:
please do not suggest putting pkg.func() in the test.py under if __name__ == '__main__'. I know it will work. I'm finding a solution without using if __name__ == '__main__'.
EDIT2:
I'm the developer of pkg package, not the user. So, I would like to resolve that issue by revising pkg package so that users of pkg package will have no restriction when using pkg.