#process.py
import multiprocessing
import os
import sys
def worker():
print 'worker'
def main():
processes = []
for i in range(2):
p = multiprocessing.Process(target=worker )
processes.append(p)
for i in range(2):
processes[i].start()
for i in range(2):
processes[i].join()
if __name__ == '__main__':
main()
#########################################
以下是打包文件代码:
#setup.py
from distutils.core import setup
import py2exe
setup(
console=[{"script": "process.py"}]
)
运行打包后的程序,报以下错误,且起了几个进程,就会报以下几个相同的错误,是不由多进程引起的,请问该如何修改?
Traceback (most recent call last):
File "process.py", line 19, in <module>
File "process.py", line 14, in main
File "multiprocessing\process.pyc", line 104, in start
File "multiprocessing\forking.pyc", line 220, in __init__
File "multiprocessing\forking.pyc", line 320, in get_command_line
RuntimeError:
Attempt to start a new process before the current process
has finished its bootstrapping phase.
This probably means that you are on Windows 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 a Windows executable.
2 个解决方案
#1
报错讯息不是告诉你应该调用freeze_support(),照着做试试先...
#2
解决啦
import multiprocessing
from multiprocessing import Process, freeze_support
import os
import sys
def worker():
print 'worker'
def main():
processes = []
for i in range(2):
p = multiprocessing.Process(target=worker )
processes.append(p)
for i in range(2):
processes[i].start()
for i in range(2):
processes[i].join()
if __name__ == '__main__':
freeze_support()
main()
#1
报错讯息不是告诉你应该调用freeze_support(),照着做试试先...
#2
解决啦
import multiprocessing
from multiprocessing import Process, freeze_support
import os
import sys
def worker():
print 'worker'
def main():
processes = []
for i in range(2):
p = multiprocessing.Process(target=worker )
processes.append(p)
for i in range(2):
processes[i].start()
for i in range(2):
processes[i].join()
if __name__ == '__main__':
freeze_support()
main()