2017-03-22 2 views
0

Simpy를 사용하여 도시 그리드 주변에서 움직이는 자동차의 동작을 모델링하려고합니다. 그러나, 나는 그냥 방법 self.someMethod()를 호출 대Simpy - yield를 사용할시기와 함수를 호출하는시기

yield self.env.timeout(delay) 또는 yield env.process(self.someMethod()) 같은 것을 사용하는 경우 개념적으로 주위에 내 머리를 감싸는 약간의 문제가 발생하고있다.

매우 이론적 인 수준에서 나는 iterables에 적용하는 방법에 관해서는 yield 진술과 생성자를 이해하지만 Simpy과 어떤 관련이 있는지 잘 모르겠습니다.

자습서는 여전히 매우 밀도가 있습니다. 예를 들어

: 당신은 아직 완전히 발전기/비동기 기능을 이해하지 못했다처럼

class Car(object): 
    def __init__(self, env, somestuff): 
     self.env = env 
     self.somestuff = somestuff 

     self.action = env.process(self.startEngine()) # why is this needed? why not just call startEngine()? 

    def startEngine(self): 
     #start engine here 
     yield self.env.timeout(5) # wait 5 seconds before starting engine 
     # why is this needed? Why not just use sleep? 



env = simpy.Environment() 
somestuff = "blah" 
car = Car(env, somestuff) 
env.run() 
+0

댓글이 없습니다'//'와'# '로 시작합니다. –

+0

죄송합니다. 코드에 붙여 넣은 후 주석을 추가했습니다. – noblerare

답변

2

것 같습니다. 나는 아래의 코드를 언급하고 당신이 무슨 일이 일어나고 있는지 이해하는 하는 데 도움이되기를 바랍니다 : 파이썬에서

import simpy 

class Car(object): 
    def __init__(self, env, somestuff): 
     self.env = env 
     self.somestuff = somestuff 

     # self.startEngine() would just create a Python generator 
     # object that does nothing. We must call "next(generator)" 
     # to run the gen. function's code until the first "yield" 
     # statement. 
     # 
     # If we pass the generator to "env.process()", SimPy will 
     # add it to its event queue actually run the generator. 
     self.action = env.process(self.startEngine()) 

    def startEngine(self): 
     # "env.timeout()" returns a TimeOut event. If you don't use 
     # "yield", "startEngine()" returns directly after creating 
     # the event. 
     # 
     # If you yield the event, "startEngine()" will wait until 
     # the event has actually happend after 5 simulation steps. 
     # 
     # The difference to time.sleep(5) is, that this function 
     # would block until 5 seconds of real time has passed. 
     # If you instead "yield event", the yielding process will 
     # not block the whole thread but gets suspend by our event 
     # loop and resumed once the event has happend. 
     yield self.env.timeout(5) 


env = simpy.Environment() 
somestuff = "blah" 
car = Car(env, somestuff) 
env.run() 
+0

이것은 나를 도와줍니다. 고맙습니다! – noblerare

관련 문제