赞
踩
在使用python多进程multiprocessing
过程中,报错
该错误表明是文件读写发生错误。这是由于PyCharm等编辑器开启进程时是需要读取代码源文件的缘故。
# main.py import os import time import multiprocessing def worker(process_num): """进程的工作函数""" print(f"Worker {process_num} is running with PID: {os.getpid()}") time.sleep(0.2) return os.getpid() if __name__ == "__main__": # 创建多个进程 num_processes = 4 processes = [] for i in range(num_processes): process = multiprocessing.Process(target=worker, args=(i,)) processes.append(process) # 启动所有进程 for process in processes: process.start() # 等待所有进程结束 for process in processes: process.join() print("All processes have finished.")
此时需要开启子进程的函数文件和主进程在同一个文件中
# worker_module.py import os import time def worker(process_num): """进程的工作函数""" print(f"Worker {process_num} is running with PID: {os.getpid()}") time.sleep(0.2) return os.getpid() # main.py import multiprocessing import worker_module if __name__ == "__main__": # 创建多个进程 num_processes = 4 processes = [] for i in range(num_processes): process = multiprocessing.Process(target=worker_module.worker, args=(i,)) processes.append(process) # 启动所有进程 for process in processes: process.start() # 等待所有进程结束 for process in processes: process.join() print("All processes have finished.")
将需要开启子进程的代码单独放入文件worker_module.py,再在主进程代码main.py中导入。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。