zoukankan      html  css  js  c++  java
  • python3 window和linux两个不同操作系统创建进程的区别

    # coding:utf-8
    import os
    import time
    from multiprocessing import Process
    
    
    def func():
        print("func start...")
        time.sleep(1)
        print("func end...")
    
    
    print("程序开始了.")
    p = Process(target=func,)
    p.start()
    print("程序结束了.")

    运行结果:

    程序开始了.
    程序结束了.
    程序开始了.
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "F:2019 pythonpython-3.4.3rc1libmultiprocessingspawn.py", line 106, in spawn_main
        exitcode = _main(fd)
      File "F:2019 pythonpython-3.4.3rc1libmultiprocessingspawn.py", line 115, in _main
        prepare(preparation_data)
      File "F:2019 pythonpython-3.4.3rc1libmultiprocessingspawn.py", line 226, in prepare
        _fixup_main_from_path(data['init_main_from_path'])
      File "F:2019 pythonpython-3.4.3rc1libmultiprocessingspawn.py", line 278, in _fixup_main_from_path
        run_name="__mp_main__")
      File "F:2019 pythonpython-3.4.3rc1lib unpy.py", line 240, in run_path
        pkg_name=pkg_name, script_name=fname)
      File "F:2019 pythonpython-3.4.3rc1lib unpy.py", line 96, in _run_module_code
        mod_name, mod_spec, pkg_name, script_name)
      File "F:2019 pythonpython-3.4.3rc1lib unpy.py", line 85, in _run_code
        exec(code, run_globals)
      File "F:2019老男孩周末26期day09课下练习1 window和linux的区别.py", line 15, in <module>
        p.start()
      File "F:2019 pythonpython-3.4.3rc1libmultiprocessingprocess.py", line 105, in start
        self._popen = self._Popen(self)
      File "F:2019 pythonpython-3.4.3rc1libmultiprocessingcontext.py", line 212, in _Popen
        return _default_context.get_context().Process._Popen(process_obj)
      File "F:2019 pythonpython-3.4.3rc1libmultiprocessingcontext.py", line 313, in _Popen
        return Popen(process_obj)
      File "F:2019 pythonpython-3.4.3rc1libmultiprocessingpopen_spawn_win32.py", line 34, in __init__
        prep_data = spawn.get_preparation_data(process_obj._name)
      File "F:2019 pythonpython-3.4.3rc1libmultiprocessingspawn.py", line 144, in get_preparation_data
        _check_not_importing_main()
      File "F:2019 pythonpython-3.4.3rc1libmultiprocessingspawn.py", line 137, in _check_not_importing_main
        is not going to be frozen to produce an executable.''')
    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.

    这是window操作系统运行下的结果.

    解决方法就是将主程序放到if __name__ == '__main__':

    # coding:utf-8
    import os
    import time
    from multiprocessing import Process
    
    
    def func():
        print("func start...")
        time.sleep(1)
        print("func end...")
    
    
    if __name__ == '__main__':
        print("程序开始了.")
        p = Process(target=func,)
        p.start()
        print("程序结束了.")
    
    
    # 程序开始了.
    # 程序结束了.
    # func start...
    # func end...
  • 相关阅读:
    ios 开发日记 15
    ios开发日记
    ios 开发日记 13-剖析网易新闻标签栏视图切换(addChildViewController属性介绍)
    ios开发日记-12 sdwebImage内存泄露
    ios 开发日记 9
    ios开发日记11 对tableView三种计算动态行高方法的分析
    ios开发日记 10- CGPathAddArc和CGPathAddArcToPoint函数
    ios开发日记10
    关于appStore不显示构建版本的问题
    UITextField/UITextView限制字数
  • 原文地址:https://www.cnblogs.com/lilyxiaoyy/p/10964552.html
Copyright © 2011-2022 走看看