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...
  • 相关阅读:
    oracle导入脚本sh
    spring事件
    mysqls数据库中 数据存在则更新,不存在则插入
    mysql中字符串的数据类型
    PHP实现从文本域textarea输入数据库并保持格式输出到html页面
    PHP实现返回上一页不刷新 和刷新的方法
    KSweb 中如何使用桌面的navicat链接数据库?
    KSweb不能上传文档?
    错误:Parse error: syntax error, unexpected '[' in D:phpStudyWWWdw_newplug-in
    PHP在使用MVC模式编写页面时,include的view页面后加载不上CSS样式问题的原因及解决方法?
  • 原文地址:https://www.cnblogs.com/lilyxiaoyy/p/10964552.html
Copyright © 2011-2022 走看看