from concurrent.futures import ProcessPoolExecutor import ctypes from multiprocessing import Manager, Lock import os # 创建 Manager 和 Lock manager = Manager() m = manager.Value(ctypes.c_int, 0) lock = manager.Lock() def calc_number(x: int, y: int, _m, total_tasks: int, _lock): """模拟耗时任务函数""" # 模拟耗时计算 res = x ** y # 用锁来保证原子操作 with _lock: _m.value += 1 current_value = _m.value # 当总任务数量和_m.value相等的时候, 通知第三方任务全部做完了 if current_value == total_tasks: print(True) print(f"m_value: {current_value}, p_id: {os.getpid()}, res: {res}") def main(): # 任务参数 t1 = (100, 200, 300, 400, 500, 600, 700, 800) t2 = (80, 70, 60, 50, 40, 30, 20, 10) len_t = len(t1) # 多进程执行任务 with ProcessPoolExecutor(max_workers=len_t) as executor: for x, y in zip(t1, t2): executor.submit(calc_number, x, y, m, len_t, lock) if __name__ == "__main__": main()