Python multi-process concurrency (multiprocessing) Usage example detailed

Source: Internet
Author: User

Http://www.jb51.net/article/67116.htm

This example describes the Python multi-process concurrency (multiprocessing) usage. Share to everyone for your reference. The specific analysis is as follows:

Due to the limitations of Python design (I'm talking about the CPython we used). You can use up to 1 CPU cores.
Python provides a very useful multi-process package multiprocessing, you only need to define a function, Python will do everything else for you. With this package, you can easily convert from single-process to concurrent execution.

1. Create a single process

If we create a few new processes, we can do the following:

?
1234567891011 import multiprocessingimport timedef 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. Using Process Pools

Yes, you are not mistaken, not a thread pool. It lets you run over multicore CPUs, and it's easy to use.

Note that with Apply_async, if you fall into async, it becomes a blocking version.

Processes=4 is the maximum number of concurrent processes.

?
1234567891011121314 import multiprocessingimport timedef 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 pool, and need to pay attention to the results

More often than not, we need to focus not only on multi-process execution, but also on the execution results of each process, as follows:

?
123456789101112131415161718 import multiprocessingimport timedef func(msg):  for i in xrange(3):    print msg    time.sleep(1)  return "done " + msgif __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."

Hopefully this article will help you with Python programming.

Python multi-process concurrency (multiprocessing) Usage example detailed

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.