main.py 852 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env python3
  2. import threading
  3. from datetime import datetime
  4. from async_realtime_environment import AsyncRealtimeEnvironment
  5. from nicegui import app, ui
  6. lblTick = ui.label()
  7. lblColor = ui.label()
  8. def clock(env):
  9. while True:
  10. lblTick.set_text(datetime.now().strftime("%H:%M:%S"))
  11. # lblTick.set_text(f"{env.now}")
  12. yield env.timeout(1)
  13. def traffic_light(env):
  14. while True:
  15. lblColor.set_text("GREEN")
  16. yield env.timeout(30)
  17. lblColor.set_text("YELLOW")
  18. yield env.timeout(5)
  19. lblColor.set_text("RED")
  20. yield env.timeout(20)
  21. async def run_simpy():
  22. env = AsyncRealtimeEnvironment(factor=0.1)
  23. env.process(traffic_light(env))
  24. env.process(clock(env))
  25. await env.run(until=300)
  26. print("Simulation complete")
  27. ui.timer(0, run_simpy, once=True)
  28. ui.run()