Python multi-process multiprocessing, multiprocessing
Import multiprocessingimport time # A specific processing function that processes 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, and execute the pool = multiprocessing in parallel. pool (processes = 4) # Add the running result to a list and follow the execution result of each process. result = [] # produce msg, and add the process pool for I in range (10): msg = "hello % d" % (I) # apply_async. It is non-blocking and supports result return for callback result. append (pool. apply_async (func, (msg,) # Shut down the process Pool so that it does not accept the new job pool. close () # The main process blocks and waits for the child process to exit. 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 .")