标签:style blog http color os sp for strong on
用Pytho实现了一个Kahn Process Network:
思路:
用Python的list模拟queue。
每个channel一个queue
用一个list (fgLog)来记录所有push到fg channel的值用于最后的显示
channel的queue设置为全局变量
代码实现:
h1f=[];fg=[];gh1=[];gh0=[];h0f=[] fgLog = [] h1FirstRun = True h0FirstRun = True fLastChoose = 0 gLastChoose = 0 def h1(): global h1FirstRun if h1FirstRun: h1FirstRun = False global h1f h1f.append(1) else: global gh1 if len(gh1) != 0: value = gh1.pop(0) global h1f h1f.append(value) def h0(): global h0FirstRun if h0FirstRun: h0FirstRun = False global h0f h0f.append(0) else: global gh0 if len(gh0) != 0: value = gh0.pop(0) global h0f h0f.append(value) def f(): global fLastChoose if fLastChoose == 0: global h1f if len(h1f) != 0: value = h1f.pop(0) global fg global fgLog fg.append(value) fgLog.append(value) fLastChoose = 1 else: global h0f if len(h0f) != 0: value = h0f.pop(0) global fg global fgLog fg.append(value) fgLog.append(value) fLastChoose = 0 def g(): global fg if len(fg) != 0: global gLastChoose value = fg.pop(0) if gLastChoose == 0: global gh1 gh1.append(value) gLastChoose = 1 else: global gh0 gh0.append(value) gLastChoose = 0 if __name__ == ‘__main__‘: runOrder = ‘order3‘ print runOrder if runOrder == ‘order1‘: for i in range(50): h1();h0();f();g() elif runOrder == ‘order2‘: for i in range(50): h1();h1();h1();g();f();h0();h0(); elif runOrder == ‘order3‘: for i in range(50): f();f();g();h1();h0();h1();h0();h0();g() print fgLog
标签:style blog http color os sp for strong on
原文地址:http://www.cnblogs.com/instant7/p/4110361.html