Python implementations of Kahnprocessnetwork

Source: Internet
Author: User

A Kahn Process Network was implemented with Pytho:

Ideas:

Use the Python list to simulate the queue.

One queue per channel

Use a list (Fglog) to record all push-to-FG channel values for the final display

The channel's queue is set to a global variable

Code implementation:

h1f=[];fg=[];gh1=[];gh0=[];h0f=[]fglog=[]h1firstrun=Trueh0firstrun=Trueflastchoose=0gLastChoose=0defH1 ():GlobalH1firstrunifH1firstrun:h1firstrun=FalseGlobalh1f H1f.append (1)    Else:        GlobalGH1ifLen (GH1)! =0:value=gh1.pop (0)Globalh1f h1f.append (value)defH0 ():GlobalH0firstrunifH0firstrun:h0firstrun=FalseGlobalh0f h0f.append (0)Else:        GlobalGh0ifLen (gh0)! =0:value=gh0.pop (0)Globalh0f h0f.append (value)deff ():GlobalFlastchooseifFlastchoose = =0:Globalh1fifLen (h1f)! =0:value=h1f.pop (0)GlobalFGGlobalfglog fg.append (value) fglog.append (value) flastchoose= 1Else:        Globalh0fifLen (h0f)! =0:value=h0f.pop (0)GlobalFGGlobalfglog fg.append (value) fglog.append (value) flastchoose=0defg ():GlobalFGifLen (FG)! =0:GlobalGlastchoose Value=fg.pop (0)ifGlastchoose = =0:Globalgh1 gh1.append (value) glastchoose= 1Else:            Globalgh0 gh0.append (value) glastchoose=0if __name__=='__main__': Runorder='Order3'    PrintRunorderifRunorder = ='Order1':         forIinchRange (50): H1 (); H0 (); F (); G ()elifRunorder = ='Order2':         forIinchRange (50): H1 (); H1 (); H1 (); G (); F (); H0 (); H0 (); elifRunorder = ='Order3':         forIinchRange (50): f (); F (); G (); H1 (); H0 (); H1 (); H0 (); H0 (); G ()PrintFglog

Python implementations of Kahnprocessnetwork

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.