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...
  • 相关阅读:
    高并发系统如何设计
    PHP的垃圾回收机制(开启垃圾回收机制后的优缺点是什么)
    移动端网站如何开发(电脑端网站到手机端网站我们需要在html代码中添加哪个meta标签)
    家庭洗车APP --- Androidclient开展 之 网络框架包介绍(一)
    一天JavaScript示例-判定web页面的区域
    左右margin top问题百分比值
    Ubuntu14.04设备JDK
    三层架构,四大天王——删
    MEMO:UIButton 中的图片和标题 左对齐
    HDU 3874 离线段树
  • 原文地址:https://www.cnblogs.com/lilyxiaoyy/p/10964552.html
Copyright © 2011-2022 走看看