main.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env python3
  2. import datetime
  3. from async_realtime_environment import AsyncRealtimeEnvironment
  4. from nicegui import ui
  5. start_time = datetime.datetime.now()
  6. def clock(env):
  7. while True:
  8. simulation_time = start_time + datetime.timedelta(seconds=env.now)
  9. clock_label.set_text(simulation_time.strftime('%H:%M:%S'))
  10. yield env.timeout(1)
  11. def traffic_light(env):
  12. while True:
  13. lblColor.set_text('GREEN')
  14. yield env.timeout(30)
  15. lblColor.set_text('YELLOW')
  16. yield env.timeout(5)
  17. lblColor.set_text('RED')
  18. yield env.timeout(20)
  19. async def run_simpy():
  20. env = AsyncRealtimeEnvironment(factor=0.1) # fast forward simulation with 1/10th of realtime
  21. env.process(traffic_light(env))
  22. env.process(clock(env))
  23. await env.run(until=300) # run until 300 seconds of simulation time have passed
  24. ui.notify('Simulation completed')
  25. # create the UI
  26. clock_label = ui.label()
  27. lblColor = ui.label()
  28. # start the simpy simulation as async task in the background as soon as the UI is ready
  29. ui.timer(0, run_simpy, once=True)
  30. ui.run()