#2012-02-27 磁针石
#承接软件自动化实施与培训 验证码破解 软件破解 脚本开发 测试和python培训等
#gtalk: ouyangchongwu#gmail.com qq 37391319 博客:testing.blog.chinaunix.net
#版权所有,转载刊登请来函联系
#自动化测试和python群组:
#python qq group: 深圳自动化测试python群:113938272
#武冈深圳qq群:66250781 都梁深圳湖南户外群:49494279
#参考资料
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import multiprocessing
# 管道消费者.
def consumer(lock,pipe):
output_p, input_p = pipe
input_p.close() # 关闭管道输入口
while True:
lock.acquire()
item = output_p.recv()
lock.release()
if item == None:
break
# 处理部分
lock.acquire()
print(item)
lock.release()
# 管道生产者
def producer(sequence, input_p):
for item in sequence:
# Put the item on the queue
input_p.send(item)
if __name__ == '__main__':
# 进程数、创建管道,锁等
p_num = 2
process = []
(output_p, input_p) = multiprocessing.Pipe()
lock = multiprocessing.Lock()
# 定义消费进程
for i in range(p_num):
t =multiprocessing.Process(target=consumer,args=(lock,(output_p, input_p),))
t.daemon=True
process.append(t)
# 启动消费进程
for i in range(p_num):
process[i].start()
# 关闭输出管道,以往管道填充数据
output_p.close()
sequence = range(100) + [None]*p_num
producer(sequence, input_p)
# 数据填充完毕,打开输入管道
input_p.close()
# 等待结束
for i in range(p_num):
process[i].join()
阅读(460241) | 评论(1) | 转发(1) |