Python multi-process concurrency (multiprocessing) Usage examples

Source: Internet
Author: User
Tags join sleep

The examples in this article describe Python multiprogramming (multiprocessing) usage. Share to everyone for your reference. The specific analysis is as follows:

Because of the limitations of Python design (I'm talking about our usual cpython). Up to 1 CPU cores can be used.

Python provides a very useful multiprocessing package, and you just need to define a function that Python will do everything else for you. With this package, you can easily complete transitions from single process to concurrent execution.

1. New single process

If we create a small number of processes, we can do the following:

?

1 2 3 4 5 6 7 8 9 10 11 Import multiprocessing Import Time def func (msg): To 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 Process Pool

Yes, you're not mistaken, not a thread pool. It allows you to run a full multi-core CPU, and the method is very easy to use.

Notice to use Apply_async, if drop async, become blocked version.

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): To I in Xrange (3): Print msg time.sleep (1) If __name__ = "__main__": P Ool = multiprocessing. Pool (processes=4) for I in Xrange (a): msg = "Hello%d"% (i) Pool.apply_async (func, (msg,)) Pool.close () Pool.join () Prin T "sub-process (es) done."

3, use pool, and need to pay attention to the results

More often than not, we need to do more than just process execution, and we need to focus on the results of each process, as follows:

?

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-xrange (3): Print msg time.sleep (1) return "Done" + msg if __na me__ = = "__main__": Pool = multiprocessing. Pool (processes=4) result = [] for i in xrange: msg = "Hello%d"% (i) result.append (Pool.apply_async (func, (msg,)) PO Ol.close () Pool.join () for res in Result:print res.get () print "sub-process (es) done."

I hope this article will help you with your 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.