Discussion:
[SimPy] yielding question
Carl Morgenstern
2015-06-26 06:46:48 UTC
Permalink
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
Camilla
2015-06-26 07:20:50 UTC
Permalink
Hi Carl!

The *req *variable is assigned in the context manager, which is the code
line beginning with the *with* keyword.
with counter.request() as req.
It is prudent to use the context manager in this case, because it will
automatically release the place at the bank counter once your process is
done with it.

This line in the code
results = yield req | env.timeout(patience)
basically says that the process is suspended until either one of those two
events is completed.
At most, the customer will have to wait until *env.timeout(patience) *event
is completed.
If we did not include env.timeout(patience), the customer might have to
wait forever for a counter spot.

You can see example of this kind of OR/AND syntax used when a process is
waiting for multiple events to finish.
This page contains more examples
http://simpy.readthedocs.org/en/latest/topical_guides/events.html#waiting-for-multiple-events-at-once


Kind regards,
Camilla
Post by Carl Morgenstern
I am new to SimPy and to Python. Can somebody walk me through this?
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
# 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))
# We reneged
print('%7.4f %s: RENEGED after %6.3f' % (env.now, name, wait))
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
results= req | env.timeout(patience)
yield results
which looks, superficially, like the same thing, I get hard to understand errors.
Thanks,
Carl
------------------------------------------------------------------------------
Monitor 25 network devices or servers for free with OpManager!
OpManager is web-based network management software that monitors
network devices and physical & virtual servers, alerts via email & sms
for fault. Monitor 25 devices for free with no restriction. Download now
http://ad.doubleclick.net/ddm/clk/292181274;119417398;o
_______________________________________________
Simpy-users mailing list
https://lists.sourceforge.net/lists/listinfo/simpy-users
Continue reading on narkive:
Loading...