python 进程创建和共享内容的方法

1.使用Pool来创建进程

from multiprocessing import Pool
def f(n):
    return n*n
if __name__=="__main__":
    p=Pool(5)
    a=range(10)
    print(p.map(f,a))

p.map会将a列表中的数据依次循环给f函数。但是当函数需要两个参数时,该方法不适用。

2.使用Process来创建进程

def info(title):
    print title
    print 'module name:', __name__
    if hasattr(os, 'getppid'):  # only available on Unix
        print 'parent process:', os.getppid()
    print 'process id:', os.getpid()

def f(name):
    info('function f')
    print 'hello', name

if __name__ == '__main__':
    info('main line')
    p = Process(target=f, args=('bob',))
    p.start()
    p.join() #串行

3.使用Pool.apply_async(异步)

from multiprocessing import Pool
import time

def f(n):
    print n*n
    time.sleep(1)
    return n*n

if __name__ == '__main__':
    p = Pool(processes=5)
    res_list = []
    for i in range(10):
        res = p.apply_async(f,[i,])
        res_list.append(res)

    for item in res_list:
        print item.get()

 

共享内存的三种方法

1.使用multiprocessing中的Queue(将Queue封装了)

from multiprocessing import Process, Queue
import Queue as Q2
def f(q,n):
    q.put([n, 'hello'])
    print q.get() 
if __name__ == '__main__':
    q = Q2.Queue()
    for i in range(5):
        p = Process(target=f, args=(q,i))
        p.start()
 #   print q.get()    # prints "[42, None, 'hello']"

from multiprocessing import Process, Lock
import time
def f(i):
  #  l.acquire()
    time.sleep(1)
    print 'hello world', i
   # l.release()

if __name__ == '__main__':
   # lock = Lock()
    for num in range(10):
        Process(target=f, args=[num]).start()

2.使用values和array(灵活性较差,不推荐使用)

from multiprocessing import Process, Value, Array

def f(n, a):
    n.value = 3.1415927
    for i in range(len(a)):
        a[i] = -a[i]

if __name__ == '__main__':
    num = Value('d', 0.0)
    arr = Array('i', range(10))

    p = Process(target=f, args=(num, arr))
    p.start()
    p.join()

    print num.value
    print arr[:]

3.使用Manager

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

 

posted @ 2017-02-25 17:09  小聪傻大  阅读(1772)  评论(0编辑  收藏  举报