首页 > 代码库 > KahnProcessNetwork的Python实现

KahnProcessNetwork的Python实现

用Pytho实现了一个Kahn Process Network:

思路:

用Python的list模拟queue。

每个channel一个queue

用一个list (fgLog)来记录所有push到fg channel的值用于最后的显示

channel的queue设置为全局变量

代码实现:

h1f=[];fg=[];gh1=[];gh0=[];h0f=[]fgLog = []h1FirstRun = Trueh0FirstRun = TruefLastChoose = 0gLastChoose = 0def 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 = 0def 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 = 0if __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                                    

 

KahnProcessNetwork的Python实现