请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

宁好 我使用python2.7执行您写的进程代码报错

环境:python2.7

执行代码块:

from multiprocessing import Process
#
def foo(i):
   print"This is Process "

for i in range(5):
   p = Process(target=foo, args=(i,))
   p.start()

报错信息:

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 509, in prepare

    '__parents_main__', file, path_name, etc

  File "G:\��ɵĹ���\python basic\Process thread co process.py", line 26, in <module>

    p.start()

  File "C:\Python27\lib\multiprocessing\process.py", line 130, in start

    self._popen = Popen(self)

  File "C:\Python27\lib\multiprocessing\forking.py", line 258, in __init__

    cmd = get_command_line() + [rhandle]

  File "C:\Python27\lib\multiprocessing\forking.py", line 358, in get_command_line

    is not going to be frozen to produce a Windows executable.''')

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.

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 509, in prepare

    '__parents_main__', file, path_name, etc

  File "G:\��ɵĹ���\python basic\Process thread co process.py", line 26, in <module>

    p.start()

  File "C:\Python27\lib\multiprocessing\process.py", line 130, in start

    self._popen = Popen(self)

  File "C:\Python27\lib\multiprocessing\forking.py", line 258, in __init__

    cmd = get_command_line() + [rhandle]

  File "C:\Python27\lib\multiprocessing\forking.py", line 358, in get_command_line

    is not going to be frozen to produce a Windows executable.''')

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.

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 509, in prepare

    '__parents_main__', file, path_name, etc

  File "G:\��ɵĹ���\python basic\Process thread co process.py", line 26, in <module>

    p.start()

  File "C:\Python27\lib\multiprocessing\process.py", line 130, in start

    self._popen = Popen(self)

  File "C:\Python27\lib\multiprocessing\forking.py", line 258, in __init__

    cmd = get_command_line() + [rhandle]

  File "C:\Python27\lib\multiprocessing\forking.py", line 358, in get_command_line

    is not going to be frozen to produce a Windows executable.''')

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.

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 509, in prepare

    '__parents_main__', file, path_name, etc

  File "G:\��ɵĹ���\python basic\Process thread co process.py", line 26, in <module>

    p.start()

  File "C:\Python27\lib\multiprocessing\process.py", line 130, in start

    self._popen = Popen(self)

  File "C:\Python27\lib\multiprocessing\forking.py", line 258, in __init__

    cmd = get_command_line() + [rhandle]

  File "C:\Python27\lib\multiprocessing\forking.py", line 358, in get_command_line

    is not going to be frozen to produce a Windows executable.''')

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.

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 509, in prepare

    '__parents_main__', file, path_name, etc

  File "G:\��ɵĹ���\python basic\Process thread co process.py", line 26, in <module>

    p.start()

  File "C:\Python27\lib\multiprocessing\process.py", line 130, in start

    self._popen = Popen(self)

  File "C:\Python27\lib\multiprocessing\forking.py", line 258, in __init__

    cmd = get_command_line() + [rhandle]

  File "C:\Python27\lib\multiprocessing\forking.py", line 358, in get_command_line

    is not going to be frozen to produce a Windows executable.''')

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.


正在回答 回答被采纳积分+3

3回答

毫末 2018-02-26 13:21:43

windows上需要面函数,

可以参考:

http://blog.csdn.net/yanhuatangtang/article/details/75270655


from multiprocessing import Process  

#window不可以用fork(),但是可以用multiprocessing,可以跨平台使用  

import time  

  

def test():  

    print("hello")  

  

if __name__=='__main__':  

    #freeze__support()  

    p=Process(target=test)  

    p.start()  

#所有的进程结束后,程序才结束  


1 回复 有任何疑惑可以回复我~
  • 谢谢老师的解答,这个也解决了我的问题
    回复 有任何疑惑可以回复我~ 2018-05-31 23:16:10
提问者 bob_jie 2018-02-26 20:43:42

感谢老师 我明天测试下

0 回复 有任何疑惑可以回复我~
毫末 2018-02-26 13:18:23

加以下一段试试

import parallelTestModule

if __name__ == '__main__':    
   extractor = parallelTestModule.ParallelExtractor()
   extractor.runInParallel(numProcesses=2, numThreads=4)


参考,https://stackoverflow.com/questions/18204782/runtimeerror-on-windows-trying-python-multiprocessing

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信