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

posted @   不能说的秘密  阅读(3000)  评论(1编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示