Carl Morgenstern
2015-06-26 06:46:48 UTC
I am new to SimPy and to Python. Can somebody walk me through this?
From the SimPy examples, the bank renege example contains this code:
with counter.request() as req:
patience = random.uniform(MIN_PATIENCE, MAX_PATIENCE)
# Wait for the counter or abort at the end of our tether
results = yield req | env.timeout(patience)
wait = env.now - arrive
if req in results:
# We got to the counter
print('%7.4f %s: Waited %6.3f' % (env.now, name, wait))
tib = random.expovariate(1.0 / time_in_bank)
yield env.timeout(tib)
print('%7.4f %s: Finished' % (env.now, name))
else:
# We reneged
print('%7.4f %s: RENEGED after %6.3f' % (env.now, name, wait))
I donât understand the syntax or semantics of this statement:
results = yield req | env.timeout(patience)
How does this parse and execute?
âenv.timeout(patience)â is an event? What type is âreqâ? Are we really computing a bitwise OR of those two entities?
Then, (req | env.timeout(patience)) is returned (yielded) back to the caller but what sort of assignment is going on? If I try this:
results= req | env.timeout(patience)
yield results
which looks, superficially, like the same thing, I get hard to understand errors.
Thanks,
Carl
From the SimPy examples, the bank renege example contains this code:
with counter.request() as req:
patience = random.uniform(MIN_PATIENCE, MAX_PATIENCE)
# Wait for the counter or abort at the end of our tether
results = yield req | env.timeout(patience)
wait = env.now - arrive
if req in results:
# We got to the counter
print('%7.4f %s: Waited %6.3f' % (env.now, name, wait))
tib = random.expovariate(1.0 / time_in_bank)
yield env.timeout(tib)
print('%7.4f %s: Finished' % (env.now, name))
else:
# We reneged
print('%7.4f %s: RENEGED after %6.3f' % (env.now, name, wait))
I donât understand the syntax or semantics of this statement:
results = yield req | env.timeout(patience)
How does this parse and execute?
âenv.timeout(patience)â is an event? What type is âreqâ? Are we really computing a bitwise OR of those two entities?
Then, (req | env.timeout(patience)) is returned (yielded) back to the caller but what sort of assignment is going on? If I try this:
results= req | env.timeout(patience)
yield results
which looks, superficially, like the same thing, I get hard to understand errors.
Thanks,
Carl