I am working on a assignment and I have to use SimPy for it. Basically there is an algorithm I have to implement and this algorithm consists of two procedures (basically two different functions). After first procedure is completed, second one has to start. I couldn't find a way to do it?
import random
import networkx as nx
from distsim import *
import matplotlib.pyplot as plt
class Alg1(Node):
def __init__(self,*args):
Node.__init__(self, *args)
self.dist = 0
self.dists = {}
self.all_dists = {}
self.time_stamp_one = 0
self.vel = 10
self.procedure_1_proc = self.env.process(self.procedure_1())
self.procedure_2_proc = self.env.process(self.procedure_2())
def procedure_1(self):
*This function includes procedure_1's operations
*This one has to start first and shouldn't be interrupted until its done
def procedure_2(self):
*This function includes procedure_2's operations
*After procedure_1 is completed, this function will take the rest
I have tried to use env.timeout() function inside procedure_2 for a sufficient time to procedure_1 complete however it didn't work and procedure_2 interrupted in every iteration.
Based on @Michael's advice I've made the change below:
def run(self):
print("------RUN1--------")
self.procedure_1_proc = self.env.process(self.procedure_1())
yield self.env.process(self.procedure_1())
print("------RUN2--------")
self.procedure_2_proc = self.env.process(self.procedure_2())
yield self.env.process(self.procedure_2())
Basically I am creating and yielding processes sequentially. But in this case, procedure_1 starts and works correctly but after it procedure_2 doesn't start. Why is that happening?
Below shows the programs output. It seems that before procedure_1() starts, it enters run() 4 times but never moves on after the first yield.
------RUN1--------
------RUN1--------
------RUN1--------
------RUN1--------
----------PROCEDURE1--------------
0: TIMEOUT received from 0 at time 2
----------PROCEDURE1--------------
0: TIMEOUT received from 0 at time 2
----------PROCEDURE1--------------
1: PROBE received from 0 at time 3
{0: 10.0}
----------PROCEDURE1--------------
1: PROBE received from 0 at time 3
----------PROCEDURE1--------------
0: REPLY received from 1 at time 4
{1: 10.0}
----------PROCEDURE1--------------
0: PROBE received from 1 at time 4
----------PROCEDURE1--------------
2: PROBE received from 1 at time 4
{1: 10.0}
----------PROCEDURE1--------------
1: REPLY received from 2 at time 5
{0: 10.0, 2: 10.0}
----------PROCEDURE1--------------
1: PROBE received from 2 at time 5
----------PROCEDURE1--------------
3: PROBE received from 2 at time 5
{2: 10.0}
----------PROCEDURE1--------------
2: REPLY received from 3 at time 6
{1: 10.0, 3: 10.0}
----------PROCEDURE1--------------
2: PROBE received from 3 at time 6
try this
also drop these lines in
__init__()
If this does not work, you may have other issues with your code. Can you post a full example of your code. How are you calling run()?