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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM