对流的合并,有时各个流的进度需要根据数据的不同而定。
def joinstreams(instreams,getnext=lambda data,livings:livings):
'''把各流进行关联,instreams是流数组,
getnext函数决定从哪些流中取下一批数据,输入参数为流编号,输出应为需要从哪些流编号读数据的数组'''
data = [ () for s in instreams ]
livings = list(range(len(instreams)))
while True:
got = []
while not got:
if not livings:
raise StopIteration
togets = getnext(data,livings)
for toget in togets:
try:
data[toget] = instreams[toget].next()
got.append(toget)
except:
livings.remove(toget)
yield data,got
#测试数据 cat data.txt
a 1
b 3
c 5
d 7 cat data2.txt
x 2
y 4
z 6