How to create a process using multiprocessing in Python

Source: Internet
Author: User

How to create a process using multiprocessing in Python

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.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

[Root @ localhost ~] # Cat twoproces. py

#! /Usr/bin/env python

From multiprocessing import Process

Import OS

Def 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:

?

1

2

3

4

[Root @ localhost ~] #./Twoproces. py

I am parent 7656

My pid is: 7660

My parent is: 7656

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

[Root @ localhost ~] # Cat badprocessID. py

#! /Usr/bin/env python

From multiprocessing import Process

Import OS

Def 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.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

[Root @ localhost ~] # Cat simplechina. py

#! /Usr/bin/env python

Import OS

From multiprocessing import Process

Def output (howworkflow, childpid ):

Info = (howmany, OS. getpid (), OS. getppid (), childpid)

Print "I: % d process ID: % d parent ID % d child ID % d \ n" % info

Def chain (howchain ):

Howmany = howmany-1

If howtasks> 0:

P = Process (target = chain, args = (howmany ,))

P. start ()

Output (howmany, p. pid)

Def main ():

Childpid = 0

Howtasks = 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:

?

1

2

3

4

5

6

[Root @ localhost ~] #./Simplechina. py

I: 5 process ID: 13581 parent ID13580 child ID 13582

I: 4 process ID: 13582 parent ID13581 child ID 13583

I: 3 process ID: 13583 parent ID13582 child ID 13584

I: 2 process ID: 13584 parent ID13583 child ID 13585

I: 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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

[Root @ localhost ~] # Cat simplefan. py

#! /Usr/bin/env python

Import OS

From multiprocessing import Process

Def output (howworkflow, childpid ):

Info = (howmany, OS. getpid (), OS. getppid (), childpid)

Print "I: % d process ID: % d parent ID % d child ID % d \ n" % info

Def 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:

?

1

2

3

4

5

6

7

[Root @ localhost ~] #./Simplefan. py

I: 0 process ID: 13594 parent ID13593 child ID 13594

I: 2 process ID: 13596 parent ID13593 child ID 13596

I: 3 process ID: 13597 parent ID13593 child ID 13597

I: 1 process ID: 13595 parent ID13593 child ID 13595

I: 4 process ID: 13598 parent ID13593 child ID 13598

I: 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.

Related Article

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.