. 1 # - * - Coding: UTF-. 8 - * - 2 Import Time . 3 from multiprocessing Import Pool . 4 DEF RUN (Fn): . 5 # Fn: function parameter is a data element of the list . 6 the time.sleep (. 1 ) . 7 Print ( * Fn Fn) . 8 . 9 IF the __name__ == " __main__ " : 10 testFL = [1,2,3,4,5,6 ] . 11 Print ( ' order: ' ) # execution order (i.e. serial execution, single-process ) 12 is S =the time.time () 13 is for Fn in testFL: 14 RUN (Fn) 15 T1 = the time.time () 16 Print ( " execution order Time: " , int (T1 - S)) . 17 18 is Print ( ' Concurrent: ' ) # create multiple processes in parallel execution 19 the pool = pool (10) # create 10 process has a number of process pool 20 # testFL: a list of data to be processed, run: function data processing testFL list of 21 pool.map (RUN , testFL) 22 pool.close () # close the process pool, no longer accept new process 23 pool.join () # main process blocked to wait for the child process exits 24 T2 = the time.time () 25 Print ( " parallel execution time: " , int (T2-T1))
Results of the: