2017-03-22 9 views
0

Simpyを使用して都市のグリッドを移動する自動車の動作をモデル化しようとしています。しかし、私はちょうど方法self.someMethod()を呼び出す対Simpy - 歩留まりを使用するタイミングと関数を呼び出すタイミング

yield self.env.timeout(delay)またはyield env.process(self.someMethod()) ようなものを使用する際に概念的周り私の頭をラップするいくつかの問題を抱えています。

非常に理論的なレベルで、私はyieldステートメントとジェネレーターをどのようにiterablesに適用するかについては理解していますが、それがどのようにSimpyに関係するかはわかりません。

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

おっと、ありがとう。そのコードをSOに貼り付けた後でそのコメントを追加しました。 – noblerare

答えて

2

それが見えます。私は以下のコードをコメントし、それはあなたが何が起こっているか理解するのに役立ちます ことを願っています:Pythonで

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

関連する問題