Python multi-process concurrency (multiprocessing)

Source: Internet
Author: User


A Manager returned by manager () would support types list, dict, Namespace, Lock, Rlock, Semaphore, Boundedsemaphore, Condit Ion, Event, Queue, Value and Array. For example,


From multiprocessing import Process, Manager


def f (D, L):

D[1] = ' 1 '

d[' 2 '] = 2

D[0.25] = None

L.reverse ()


if __name__ = = ' __main__ ':

Manager = Manager ()


D = manager.dict ()

L = manager.list (range (10))


p = Process (Target=f, args= (d, L))

P.start ()

P.join ()


Print D

Print L

would print


{0.25:none, 1: ' 1 ', ' 2 ': 2}

[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]



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


Use pool to focus on results


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



#!/usr/bin/env python

#coding =utf-8

"""

Author:squall

Last modified:2011-10-18 16:50

Filename:pool.py

Description:a simple Sample for Pool class

"""


From multiprocessing import Pool

From time import sleep


def f (x):

For I in range (10):

Print '%s---%s '% (i, X)

#sleep (1)



def main ():

Pool = Pool (processes=3) # Set the processes max number 3

For I in Range (11,20):

result = Pool.apply_async (f, (I,))

Pool.close ()

Pool.join ()

If Result.successful ():

print ' successful '



if __name__ = = "__main__":

Main ()


Create a process pool with a capacity of 3, and then pass F (i) to it in turn, using PS aux after running the script | grep pool.py looks at the process and finds that only three processes are executing. Pool.apply_async () is used to submit a target request to the process pool, pool.join () is used to wait for the worker process in the process pool to complete, preventing the main process from ending before the worker process ends. However, the Pool.join () must be used after pool.close () or pool.terminate (). The difference between close () and terminate () is that close () waits for the worker process in the pool to end and then close the pool, while terminate () is closed directly. Result.successful () indicates the state of the entire invocation execution, and throws a Assertionerror exception if there are still workers that have not finished executing.



This article is from the "Big Barren Sutra" blog, please be sure to keep this source http://2892931976.blog.51cto.com/5396534/1761762

Python multi-process concurrency (multiprocessing)

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.