Python more complicated process (multiprocessing) Usage example explanation

This paper describes examples of Python more complicated process (multiprocessing) usage. Share to you for your reference. Specific analysis is as follows:

Because of limitations in Python (I'm talking about our common CPython). You can only use up a full CPU core.
Python provides a very easy to use multi-process package multiprocessing, you only need to define a function, Python will do all the other things for you. With this package, you can easily complete the conversion from single-process to be executed concurrently.

1, the new single process

If we create a small amount of process can be as follows:

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, the use of process pool

Yes, you read right, not a thread pool. It allows you to run over multi-core CPU, but also very simple to use.

Pay attention to use apply_async, if falling async, it becomes blocked version.

processes = 4 is the largest number of concurrent processes.

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, using the Pool, and the need to focus on results

More often, we not only need to perform multiple processes, but also need to focus on the implementation of the results of each process, as follows:

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 write to you, for everyone to recommend a very wide python learning resource gathering, click to enter , there is a senior programmer before learning to share

Experience, study notes, there is a chance of business experience, and for everyone to carefully organize a python to combat zero-based item of information,

Python day to you on the latest technology, prospects, learning to leave a message of small details
hope the paper everyone Python programming help.

Published 10 original articles · won praise 0 · Views 3952

Guess you like

Origin blog.csdn.net/haoxun11/article/details/104887027