1, because Python multi-threading suitable for multi-io operation, but not suitable for CPU computing work, this time can be achieved through a multi-process. Python multi-process simple and practical
# multi-process, can be CPU consistent, Python multithreading for multi-io. For high CPUs can be achieved through multiple processes. Import Multiprocessingimport timedef Run (name): Print ( " %s process is running " % ( Name) time.sleep ( 2 if __name__ = = " __main__ " : for i in range (1 , 10 = multiprocessing. Process (Target=run, Args= (I,)) Process.Start ()
2, Python multi-process. All processes in the operating system have a root process (process number is created at the same). Python Gets the main process number and child process number
# Multi-process, can be CPU consistent, Python multithreading for multi-io. For high CPUs can be achieved through multiple processes. Import Multiprocessingimport timeimport osdef Run (name): Print ("%s process is running"%(name)) Time.sleep (2) def info (name): Print ("%s"%(name)) print ("Current process number:%s"%(Os.getpid ())) print ("Parent Process number:%s"%(Os.getppid ()))if__name__ = ='__main__': info ("Parent Process") Process= multiprocessing. Process (target=info,args= ("Child Process",)) Process.Start () E:\Users\xiajinqi\PycharmProjects\Atm\venv\Scripts\python.exe E:/users/xiajinqi/pycharmprojects/atm/333. Pyparent process Current Progress number:23420Parent Process Number:2372Child process Current progress number:23468Parent Process Number:23420Process finished with exit code0
3, Queue realization process communication, process Q to achieve communication. Must be indicated as process Q. Principle: When creating a sub-process, passing a Q is actually equivalent to copying a new Q-to-child process. When the child processes to the new Q input data. Process Q synchronizes the new data to the old Q (that is, the parent process Q) through an deserialization implementation. The memory between the two programs is isolated. Q is also independent of each other.
# Multi-process, can be CPU consistent, Python multithreading for multi-io. For high CPUs can be achieved through multiple processes. frommultiprocessing Import Process,queue # only named process Qimport Timeimport osqu=Queue () def test (qu): qu.put (["Clone Q"])if__name__ = ='__main__': Process= Process (target=test,args=(Qu,)) Process.Start () print (Qu.Get()) E:\Users\xiajinqi\PycharmProjects\Atm\venv\Scripts\python.exe E:/users/xiajinqi/pycharmprojects/atm/333. py['Clone Q']process finished with exit code0
Python Multi-process explanation