1

I am still quite new to python. Lately, playing with simpy module I encountered some strange behaviour by python interpreter. I implemented .flood() function and it is supposed to be a part of simulation. To my surprise, the debug inside of it was not shown on the console. Inside of this function are two major chunks of code, so I decided to erase one of them (containing while True: loop).

print("Starting program")
env = simpy.Environment()

#From now on we assume the nodes to be 0-indexed 
#Create two routers we will connect manually
routers = [Router(env, i, debug = True) for i in range(3)]

# Create wire connection between routers

for i in range(2):
    addConnection(env, routers[i], routers[i + 1])

for router in routers:
    print("Line above .flood() call")
    router.flood()

env.run(until = 100)
    def flood(self):
        print(f"beginning to flood at {self.env.now}")
        for wire in self.out_wire:
            wire.put(Packet(self.env.now,
                            random.randint(1, 10**9),
                            {"node": self.id, "neigh":self.neighbors}))

        while True:
            processes = [nei.get() for nei in self.in_wire]
            res = yield simpy.events.AnyOf(self.env, processes)
            print("RESULT: ", res, f"current time is {self.env.now}")
            for packet in res:
                print(packet)

Running this code produces:

Starting program
Line above .flood() call
Line above .flood() call
Line above .flood() call

When I comment while True: part, then the output is as follows:

Starting program
Line above .flood() call
beginning to flood at 0
Line above .flood() call
beginning to flood at 0
Line above .flood() call
beginning to flood at 0

Now I am wondering what have I done wrong? I am aware, that my simulation is still not working, and probably I am doing something not too smart there, but other than this did anyone ever see something similar?

Fly_37
  • 312
  • 1
  • 12
  • What is the purpose of the `while True:` line in the `flood()` method? – quamrana Jun 11 '21 at 15:34
  • 1
    ``flood()`` is a generator / simpy process. You have to run it via the ``env``. – MisterMiyagi Jun 11 '21 at 17:22
  • res = yield simpy.events.AnyOf(self.env, processes) -- get()s just one of your get()s in the processes list. Where do you process the rest of the get()s in your processes list? – Michael Jun 11 '21 at 20:13

1 Answers1

0

so I think if the self.in_wire is empty in

processes = [nei.get() for nei in self.in_wire]

then processes will be empty and therefore there will be no events to yield on and no res to print. No yield mean your infinite loop does not stop/pause blocking your code.

You can test this theory by adding a yield env.timeout(1) at the bottom of your loop

Michael
  • 1,671
  • 2
  • 4
  • 8
  • Well, shouldn't then the "beginning to flood" be displayed? – Fly_37 Jun 11 '21 at 20:09
  • There may be some buffering and the spinning infinite loop prevents the buffer from getting dumped to screen. try adding the yield env.timeout(1) at the end – Michael Jun 11 '21 at 20:19
  • 1
    When you commented out the while, did you comment out just the while or the whole while loop including the enclosed yield? If so then MisterMiyagi may be right and you need to call flood() as env.process(router.flood()) – Michael Jun 11 '21 at 20:29