Differences in process threads, threads, and co-routines
Linux or Unix has a fork () function, but it does not support the win system.
Multiprocessing
The multiprocessing module is a cross-platform version of a multi-process module. To support the win system, use the following:
fromMultiprocessingImportProcessImportOS#code to be executed by the child processdefRun_proc (name):Print('Run Child process%s (%s) ...'%(name, Os.getpid ()))if __name__=='__main__': Print('Parent process%s.'%os.getpid ()) P= Process (Target=run_proc, args= ('Test',)) Print('Child process would start.') P.start () P.join ()Print('Child process end.')
>>>
Parent process 9860.
Child process would start.
Run Child Process Test (9764) ...
Child process end.
* The py file is executed through a CMD window, otherwise it will not perform many processes
When you create a child process, you only need to pass in a parameter that executes functions and functions, create a process instance, and start with the start () method, so that the creation process is simpler than fork ().
The join () method can wait for the child process to end before continuing to run, typically for inter-process synchronization.
Pool
If you want to start a large number of child processes, you can create the child processes in batches using the process pool:
fromMultiprocessingImportPoolImportOS, time, RandomdefLong_time_task (name):Print("Run Task%s (%s) ...."%(name, Os.getpid ())) Start=time.time () time.sleep (Random.random ()*) End=time.time ()Print("Task%s runs%0.2f seconds."% (name, (End-start )))if __name__=="__main__": Print("Parent process%s."%os.getpid ()) P= Pool (4)#number of processes allowed to run forIinchRange (5): P.apply_async (long_time_task, args=(i,))Print("waiting-subprocesses done ....") P.close () P.join ()Print("All subprocesses is done .")
>>>
Parent process 10852.
Waiting-subprocesses done ....
Run Task 0 (9620) ....
Run Task 1 (10180) ....
Run Task 2 (8116) ....
Task 2 runs 0.03 seconds.
Run Task 3 (8116) ....
Run Task 4 (8744) ....
Task 4 runs 0.42 seconds.
Task 0 runs 0.64 seconds.
Task 1 runs 1.15 seconds.
Task 3 runs 2.90 seconds.
All subprocesses is done.
Resources:
Https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 001431927781401bb47ccf187b24c3b955157bb12c5882d000
Python Basics = = = multi-Process