Python 多进程
短信预约 -IT技能 免费直播动态提醒
#_*_coding:utf-8_*_
__author__ = 'jieli'
import time
import multiprocessing
'''
线程多锁是不需要当成参数传多,因为线程之间是共享内存多。
但是进程之间多锁是需要当成参数传的,因为进程之间的内存是独立的
多进程之间加锁是为了防止同时对一个文件做操作等等
'''
def run(num,l):
time.sleep(1)
#l.acquire()
print "hello, my name is:",num
#l.release()
if __name__ == '__main__':
lock = multiprocessing.Lock()
for i in range(20):
p = multiprocessing.Process(target=run, args=(i,lock))
p.start()
'''
#_*_coding:utf-8_*_
__author__ = 'jieli'
import time
import multiprocessing
def run(num,l,names):
time.sleep(0.5)
l.acquire()
#print "hello, my name is:",num
names.append(num)
l.release()
print names
#p = multiprocessing.Process(target=run, args=('Alex',))
#p.start()
if __name__ == '__main__':
lock = multiprocessing.Lock()
name_list = []
for i in range(20):
p = multiprocessing.Process(target=run, args=(i,lock,name_list))
p.start()
time.sleep(3)
print name_list
'''
'''
进程池,进程之间的通信
'''
def run(num,data_list):
time.sleep(2)
data_list.append(num)
print "hello, my name is:",num,data_list
if __name__ == '__main__':
m = multiprocessing.Manager()
print m
num_list = m.list()
print m.list()
p_list = []
Pool = multiprocessing.Pool(processes=4)
for i in range(5):
p = Pool.apply_async(run,args=(i,num_list))
p.get() #类似多线程的join()
# p_list.append(p)
# for p in p_list:
# p.get()
print 'num list:', num_list
###########
def run(num,data_list):
time.sleep(2)
data_list.append(num)
print "hello, my name is:",num,data_list
if __name__ == '__main__':
m = multiprocessing.Manager()
print m
num_list = m.list()
print m.list()
p_list = []
Pool = multiprocessing.Pool(processes=4)
for i in range(5):
p = Pool.apply_async(run,args=(i,num_list))
#p.get() #=join()
p_list.append(p)
for p in p_list:
p.get()
print 'num list:', num_list
'''
管道
'''
from multiprocessing import Process, Pipe,Queue
#import Queue
def f(conn,q):
conn.send([42, None, 'hello'])
conn.send('test')
q.put("hahahha")
conn.close()
if __name__ == '__main__':
A, B = Pipe()
Q = Queue()
p = Process(target=f, args=(B,Q))
p.start()
print A.recv() # prints "[42, None, 'hello']"
print A.recv() # prints "[42, None, 'hello']"
print 'from q:',Q.get()
p.join()
############
from multiprocessing import Process, Pipe,Queue
def f(conn,q):
for i in range(10):
conn.send([42, i, 'hello'])
q.put("hahahha")
conn.close()
if __name__ == '__main__':
A, B = Pipe()
Q = Queue()
p = Process(target=f, args=(B,Q))
p.start()
print A.recv() # prints "[42, 0, 'hello']"
print A.recv() # prints "[42, 1, 'hello']"
print A.recv() # prints "[42, 2, 'hello']"
print A.recv() # prints "[42, 3, 'hello']"
print A.recv() # prints "[42, 4, 'hello']"
###还可以继续print,上面发送多少个,这边就可以接受多少个。如果这里的A.recv();的数量比上面
### send()还多,那就处于阻塞状态!
print 'from q:',Q.get()
p.join()
2.多进程
import multiprocessing
p = multiprocessing.Process(targe=run,args=(arg,))
p.start()
p.join()
进程池
Pool = multiprocessing.Pool(process=5) ##是保证5个而不是最多5个,哪怕下面的range(2)也是起5个,
for i in range(10):
Pool.apply_async(run,args=(run,))
管道:
Pipe = multiprocessing.Pipe(process=5)
for i in range(2):
Pipe.applu_async(run,args=(run))
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341