title: Starting a simpy simulation run using a callback
body: <p>How can I write some simulation code (including waiting for events) from a callback?</p>
  
  <p>Reducing my process to the simplest possible example, I want to start some executable code when an event occurs in my simpy simulation. What I did was set that executable code as a callback for an event. That executable code itself has some simpy event handling in it, specifically a timeout. In my real code, this happens within a process, but this simplified version, where <code>event</code> is in main displays the same problem behavior:</p>
  
  <pre><code>import simpy
  if __name__ == '__main__':
      env = simpy.Environment()
  
      def simulationRun(event):
          print("Starting a run at time [%i]" % env.now)
          yield env.timeout(5)
          print("Ending a run at time [%i]" % env.now)
  
      event = env.timeout(5)
      event.callbacks.append(simulationRun)
  
      print("Starting simulation.")
      env.run(20)
      print("End of simulation.")
  </code></pre>
  
  <p>My expected output is:</p>
  
  <pre><code>Starting simulation.
  Starting a run at time [5]
  Ending a run at time [10]
  End of simulation.
  </code></pre>
  
  <p>However, my actual output is:</p>
  
  <pre><code>Starting simulation.
  End of simulation.
  </code></pre>
  
  <p>When I remove the yield command, I get:</p>
  
  <pre><code>Starting simulation.
  Starting a run at time [5]
  Ending a run at time [5]
  End of simulation.
  </code></pre>
  
  <p>Clearly the callback is set correctly but the whole point is to simulate the timeout in <code>simulationRun</code>. How can I run some simulation code (including waiting for an event) in code from a callback?</p>
  
  <p>This uses Python 3.4.2 and simpy.</p>