Python Multi-process explanation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.