Import multiprocessingimport time# Specific processing function, responsible for handling a single task Def func (msg): # for I in range (3): print (msg) Time.sleep (1) return "done" + msgif __name__ = = "__main__": # Process pool, create multiple processes, execute pool = multiprocessing in parallel. Pool (processes=4) # Add the results of the run to a list, focusing on the execution results of each process result = [] # Production MSG, and join the process pool for I in range: msg = "Hello%d"% (i) # Apply_async It is non-blocking and supports results returned with callback Result.append (Pool.apply_async (func, (msg,)) # Close the process pool so that it does not accept the new task pool.close () # The main process is blocked waiting for the child process to exit, and the Join method must be used after close or terminate. Pool.join () # View execution result for res in result: print (Res.get ()) print ("sub-process (es) done.")
Python Multi-process multiprocessing