zoukankan      html  css  js  c++  java
  • python 多进程代码问题以及用 pyinstaller 打包成exe 问题解决

    python 多进程运行报错concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-#
    
    # -------------------------------------------------------------------------------
    # Name:         demo
    # Author:       yunhgu
    # Date:         2021/6/25 13:41
    # Description: 
    # -------------------------------------------------------------------------------
    from concurrent.futures import ProcessPoolExecutor, as_completed
    import time
    import multiprocessing
    import os
    
    
    def job(jobid):
        print(f"start job {jobid}   time:{time.time()}  id:{os.getpid()}")
        time.sleep(2)
        print(f"{jobid} finished")
        return "success"
    
    
    multiprocessing.freeze_support()
    with ProcessPoolExecutor(max_workers=4) as p:
        task_list = [p.submit(job, job_id) for job_id in range(5)]
        for task in as_completed(task_list):
            if task.done():
                print(task.result())
    

    image

    需要添加main

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-#
    
    # -------------------------------------------------------------------------------
    # Name:         demo
    # Author:       yunhgu
    # Date:         2021/6/25 13:41
    # Description: 
    # -------------------------------------------------------------------------------
    from concurrent.futures import ProcessPoolExecutor, as_completed
    import time
    import multiprocessing
    import os
    
    
    def job(jobid):
        print(f"start job {jobid}   time:{time.time()}  id:{os.getpid()}")
        time.sleep(2)
        print(f"{jobid} finished")
        return "success"
    
    if __name__ == '__main__':
        multiprocessing.freeze_support()
        with ProcessPoolExecutor(max_workers=4) as p:
            task_list = [p.submit(job, job_id) for job_id in range(5)]
            for task in as_completed(task_list):
                if task.done():
                    print(task.result())
    

    image

    使用pyinstaller 打包成exe

    对于pyinstaller 是不支持含有多进程的python打包,打包完毕后,不会执行子进程的内容,而是会一直创建进程,导致崩溃
    解决方法:
    multiprocessing.freeze_support()
    这句话一定要放在main下的第一行,如下所示

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-#
    
    # -------------------------------------------------------------------------------
    # Name:         demo
    # Author:       yunhgu
    # Date:         2021/6/25 13:41
    # Description: 
    # -------------------------------------------------------------------------------
    from concurrent.futures import ProcessPoolExecutor, as_completed
    import time
    import multiprocessing
    import os
    
    
    def job(jobid):
        print(f"start job {jobid}   time:{time.time()}  id:{os.getpid()}")
        time.sleep(2)
        print(f"{jobid} finished")
        return "success"
    
    if __name__ == '__main__':
        multiprocessing.freeze_support()
        with ProcessPoolExecutor(max_workers=4) as p:
            task_list = [p.submit(job, job_id) for job_id in range(5)]
            for task in as_completed(task_list):
                if task.done():
                    print(task.result())
    

    然后就可以正常运行了
    image

    不论你在什么时候开始,重要的是开始之后就不要停止。 不论你在什么时候结束,重要的是结束之后就不要悔恨。
  • 相关阅读:
    B. Gerald is into Art
    day 56 Django介绍
    day 55 bootstrap应用
    Python学习之变量的作用域
    day 54 JQ用法
    day 53 BOM与DOM
    day 52 js语法
    day 51 label标签,文字属性,背景属性,边框,display属性,盒模型,浮动,overflow,定位,z-index和模态框
    day 50 form表单,input框,textarea,select,css的引入方式,标签嵌套,选择器,宽高属性,字体属性
    day 49 html标签介绍,标题、段落、a、img、表格、特殊符号、常用标签、div、span
  • 原文地址:https://www.cnblogs.com/yunhgu/p/14931682.html
Copyright © 2011-2022 走看看