Re: Holding can only be done within the PEM context. How to overcome this?
Nicolas Dandrimont <nicolas.dandrimont <at> crans.org>
2012-05-06 07:14:52 GMT
Le 06/05/2012 à 04:22, Carlos Ferreira <carlosmf.pt <at> gmail.com> écrivit :
> Hmm, maybe some miss-communication here. Sorry for that.
>
> In order for a process to suspend (wait) for some time, a yield has to be
> issued within the Process PEM. This can be a bit annoying, when simulating
> a more complex system, like Network Nodes which exchange packets.
>
> If I have a Process which represents a Network Agent and for which it's PEM
> represents it's behaviour, I am only able to suspend the process using the
> yield command when inside the PEM context. If during the PEM execution, a
> method is called for which will be responsible for example, simulating the
> sending of a packet and waiting for an other process agent to answer, I am
> unable to suspend the agent process for which it's PEM called this special
> method.
>
> So, asking in a direct way, how can I suspend a process for a certain time
> or special event, that calls from within it's PEM, methods that are
> responsible for that suspension.
Hello,
SimPy's PEMs are called in python "generators", which simply are
functions that can be interrupted and yield ("return") several values.
To write a "sub-generator", you need to consume each value it yields
with a for loop, as a for loop is the classical way to consume the
values from a generator. See the (pseudo-)code below:
---------------------------
def sub_generator():
yield "sub-a"
yield "sub-b"
yield "sub-c"
def main_generator():
yield "main-a"
for value in sub_generator():
yield value
---------------------------
which is equivalent to:
---------------------------
def main_generator():
yield "main-a"
yield "sub-a"
yield "sub-b"
yield "sub-c"
---------------------------
I think the next python 3 release includes the "yield from sub_generator()"
idiom, which is the equivalent of the for loop above and makes it cleaner.
Hope this helps,
--
--
Nicolas Dandrimont
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Simpy-users mailing list
Simpy-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/simpy-users