Python uses multiprocessing to create a process.

Source: Internet
Author: User

Python uses multiprocessing to create a process.

This example describes how to create a process using multiprocessing in Python. Share it with you for your reference. The specific analysis is as follows:

The Process can be created by calling the multiprocessing Process. The following Code creates two processes.

[root@localhost ~]# cat twoproces.py #!/usr/bin/env pythonfrom multiprocessing import Processimport osdef output(): print "My pid is :%d\n" % os.getpid() print "My parent is:%d\n" % os.getppid()def main(): p=Process(target=output)  p.start() print "I am parent %d\n" % os.getpid()if __name__=="__main__": main()

The running result is as follows:

[root@localhost ~]# ./twoproces.py I am parent 7656My pid is :7660My parent is:7656

The mypid value of the following program results will remain unchanged in the two processes.

[root@localhost ~]# cat badprocessID.py #!/usr/bin/env pythonfrom multiprocessing import Processimport osdef output(mypid): print "I am child %d ID :%d\n" % (os.getpid(),mypid)def main(): mypid=os.getpid() p=Process(target=output,args=(mypid,))  print "I am parent %d\n" % os.getpid() p.start()if __name__=="__main__": main()

Mypid has not been reset with the sub-process, so the mypid does not change during running.

The following code creates a process chain for sub-processes.

[root@localhost ~]# cat simplechina.py #!/usr/bin/env pythonimport osfrom multiprocessing import Processdef output(howmany,childpid): info = (howmany,os.getpid(),os.getppid(),childpid) print "i:%d process ID:%d parent ID%d child ID %d\n" % infodef chain(howmany): howmany = howmany-1 if howmany > 0: p = Process(target=chain,args=(howmany,))  p.start() output(howmany,p.pid)def main(): childpid = 0 howmany=6 p = Process(target=chain,args=(howmany,)) p.start()if __name__=="__main__": main()

With the main Process using Process to create A sub-Process, and sub-Process and then create A sub-Process (that is, A-> B-> C-D), and so on, know that howmany is less than 0 to end. The result of running 1 is as follows:

[root@localhost ~]# ./simplechina.py i:5 process ID:13581 parent ID13580 child ID 13582i:4 process ID:13582 parent ID13581 child ID 13583i:3 process ID:13583 parent ID13582 child ID 13584i:2 process ID:13584 parent ID13583 child ID 13585i:1 process ID:13585 parent ID13584 child ID 13586

How to create a process fan:
A
/\
B C
Create the simplefan. py file with the following code:

[root@localhost ~]# cat simplefan.py #!/usr/bin/env pythonimport osfrom multiprocessing import Processdef output(howmany,childpid): info = (howmany,os.getpid(),os.getppid(),childpid) print "i:%d process ID:%d parent ID%d child ID %d\n" % infodef fan(i): output(i,os.getpid())def main(): pList=[] for i in range(6): p = Process(target=fan,args=(i,)) pList.append(p) for p in pList: p.start() for p in pList: p.join()if __name__=="__main__": main()

The running result is as follows:

[root@localhost ~]# ./simplefan.py i:0 process ID:13594 parent ID13593 child ID 13594i:2 process ID:13596 parent ID13593 child ID 13596i:3 process ID:13597 parent ID13593 child ID 13597i:1 process ID:13595 parent ID13593 child ID 13595i:4 process ID:13598 parent ID13593 child ID 13598i:5 process ID:13599 parent ID13593 child ID 13599

Except for the process whose ID is 13593, the parent process of other processes is 13593.
The parent process ends with all sub-processes, such as the join method.

I hope this article will help you with Python programming.

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.