码迷,mamicode.com
首页 > 编程语言 > 详细

Python 多进程

时间:2016-05-10 16:46:59      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

from multiprocessing import  Process

def f(name):
print (‘hello‘,name)

if __name__ == ‘__main__‘:
p=Process(target=f,args=(‘freeman‘,))
p.start()
p.join()

from multiprocessing import Process,Value,Array

def f(n,a):
n.value=3.1415972
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[:])

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)
from multiprocessing import Pool
import time

def f(x):
time.sleep(1)
return x*x
if __name__ == ‘__main__‘:
pool = Pool(processes=8)
res_list = []
for i in range(5):
p = pool.apply_async(f,[i])
res_list.append(p)
for i in res_list:
print (i.get())

Python 多进程

标签:

原文地址:http://www.cnblogs.com/yjz1/p/5478316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!