Example of how to use Python multi-process concurrency (multiprocessing)

Source: Internet
Author: User

Example of how to use Python multi-process concurrency (multiprocessing)

This example describes how to use Python multi-process concurrency (multiprocessing. Share it with you for your reference. The specific analysis is as follows:

Due to restrictions of Python design (I am talking about CPython ). A maximum of one CPU core can be used.

Python provides a very useful multi-process package multiprocessing. You only need to define a function, and Python will do everything else for you. With this package, you can easily complete the conversion from a single process to concurrent execution.

1. Create a single process

If we create a small number of processes, we can:

?

1

2

3

4

5

6

7

8

9

10

11

Import multiprocessing

Import time

Def func (msg ):

For I in xrange (3 ):

Print msg

Time. sleep (1)

If _ name _ = "_ main __":

P = multiprocessing. Process (target = func, args = ("hello ",))

P. start ()

P. join ()

Print "Sub-process done ."

2. Use the process pool

Yes, you are not mistaken, not the thread pool. It allows you to run a full range of multi-core CPUs, and the usage is very simple.

Note that you must use apply_async. If async is dropped, the version will become blocked.

Processes = 4 is the maximum number of concurrent processes.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Import multiprocessing

Import time

Def func (msg ):

For I in xrange (3 ):

Print msg

Time. sleep (1)

If _ name _ = "_ main __":

Pool = multiprocessing. Pool (processes = 4)

For I in xrange (10 ):

Msg = "hello % d" % (I)

Pool. apply_async (func, (msg ,))

Pool. close ()

Pool. join ()

Print "Sub-process (es) done ."

3. Use the Pool and follow the results

In more cases, we not only need to execute multiple processes, but also pay attention to the execution results of each process, as shown below:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

Import multiprocessing

Import time

Def func (msg ):

For I in xrange (3 ):

Print msg

Time. sleep (1)

Return "done" + msg

If _ name _ = "_ main __":

Pool = multiprocessing. Pool (processes = 4)

Result = []

For I in xrange (10 ):

Msg = "hello % d" % (I)

Result. append (pool. apply_async (func, (msg ,)))

Pool. close ()

Pool. join ()

For res in result:

Print res. get ()

Print "Sub-process (es) done ."

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.