delay1.py 862 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import time
  2. from eventloop import EventLoop
  3. run_time = 0
  4. eventloop.set_debug(True)
  5. def test_func(arg1, arg2):
  6. global run_time
  7. run_time += 1
  8. print("Running test function with arguments:", arg1, arg2)
  9. return arg1 + arg2
  10. def test_func2(arg1, arg2):
  11. print("test function 2 with arguments:", arg1, arg2)
  12. return arg1 + arg2
  13. def test_callback(res):
  14. print("Running test callback function", res)
  15. assert res == "Hello World"
  16. # Test case 2: Add and run a periodic task
  17. eventloop.start_new_task_once(
  18. test_func, ("Hello", " World"),
  19. callback=test_callback,
  20. delay_ms=200
  21. )
  22. eventloop.start_new_task(
  23. test_func2, ("Hello", " World"),
  24. is_periodic=True,
  25. period_ms=20,
  26. callback=test_callback
  27. )
  28. # Sleep for enough time to allow the periodic task to run multiple times
  29. while run_time < 1:
  30. time.sleep(0.1)