python 多进程+超时机制+进程函数返回值

 

实现多进程,以及拿到每个进程return的返回值,并增加超时机制,如果进程超时,则立即kill

 

import os
import time
from concurrent.futures import ProcessPoolExecutor, as_completed
from multiprocessing import Process, Manager

def target_function(param):
    print(f"Processing parameter: {param} (PID: {os.getpid()})")
    time.sleep(param)
    return f"Process {param} finished"

def run_process_with_timeout(param, timeout, return_dict):
    def target_wrapper():
        return_dict[param] = target_function(param)
    
    p = Process(target=target_wrapper)
    p.start()
    p.join(timeout)
    if p.is_alive():
        p.terminate()  # Terminate the process if it exceeds timeout
        p.join()
        return False
    return True

if __name__ == "__main__":
    params = [2, 6, 3, 100]
    timeout = 5
    start = time.time()
    
    manager = Manager()
    return_dict = manager.dict()
    
    with ProcessPoolExecutor() as executor:
        futures = {executor.submit(run_process_with_timeout, param, timeout, return_dict): param for param in params}

        try:
            for future in as_completed(futures):
                param = futures[future]
                if future.result():
                    result = return_dict[param]
                    print(result)
                else:
                    print(f"Process {param} timed out")
        except Exception as e:
            print("An error occurred:", e)

    end = time.time()
    print(f"Time elapsed: {end - start:.2f} seconds")
    print("All processes have been completed.")
    print('hello')
    print('world')
    print('hello world')

 

posted @ 2025-02-20 13:36  WTSRUVF  阅读(46)  评论(0)    收藏  举报