from pyloid.timer import PyloidTimertimer_manager =PyloidTimer()
Periodic Timers
To start a timer that runs periodically, use the start_periodic_timer method:
defprint_hello():print("Hello!")# Start a timer that prints "Hello!" every 2 secondstimer_id = timer_manager.start_periodic_timer(2000, print_hello)
from pyloid.timer import PyloidTimerfrom pyloid import Pyloid# Create a PyloidTimer instanceapp =Pyloid(app_name="Pyloid-App")timer_manager =PyloidTimer()defprint_hello():print("Hello!")# Start a timer that prints "Hello!" every 2 secondstimer_id = timer_manager.start_periodic_timer(2000, print_hello)app.run()
Single-Shot Timers
To start a timer that runs only once, use the start_single_shot_timer method:
defdelayed_message():print("5 seconds have passed!")# Start a single-shot timer that prints a message after 5 secondstimer_id = timer_manager.start_single_shot_timer(5000, delayed_message)
from pyloid.timer import PyloidTimerfrom pyloid import Pyloid# Create a PyloidTimer instanceapp =Pyloid(app_name="Pyloid-App")timer_manager =PyloidTimer()defdelayed_message():print("5 seconds have passed!")# Start a single-shot timer that prints a message after 5 secondstimer_id = timer_manager.start_single_shot_timer(5000, delayed_message)app.run()
Timer Management
Stopping a Timer
# Stop a timer using its IDtimer_manager.stop_timer(timer_id)
from pyloid.timer import PyloidTimerfrom pyloid import Pyloid# Create a PyloidTimer instanceapp =Pyloid(app_name="Pyloid-App")timer_manager =PyloidTimer()# Start a timertimer_id = timer_manager.start_periodic_timer(2000, lambda: print("Hello!"))# Stop a timer using its IDtimer_manager.stop_timer(timer_id)app.run()
Checking Timer Activity
if timer_manager.is_timer_active(timer_id):print("The timer is still running.")else:print("The timer has stopped.")
from pyloid.timer import PyloidTimerfrom pyloid import Pyloid# Create a PyloidTimer instanceapp =Pyloid(app_name="Pyloid-App")timer_manager =PyloidTimer()# Start a timertimer_id = timer_manager.start_periodic_timer(2000, lambda: print("Hello!"))# Check if the timer is activeif timer_manager.is_timer_active(timer_id):print("The timer is still running.")else:print("The timer has stopped.")app.run()
Checking Remaining Time
remaining_time = timer_manager.get_remaining_time(timer_id)if remaining_time isnotNone:print(f"{remaining_time}ms left until the timer fires.")
from pyloid.timer import PyloidTimerfrom pyloid import Pyloid# Create a PyloidTimer instanceapp =Pyloid(app_name="Pyloid-App")timer_manager =PyloidTimer()# Start a timertimer_id = timer_manager.start_periodic_timer(2000, lambda: print("Hello!"))# Check remaining timeremaining_time = timer_manager.get_remaining_time(timer_id)if remaining_time isnotNone:print(f"{remaining_time}ms left until the timer fires.")app.run()
Changing Timer Interval
# Change the timer interval to 3 secondstimer_manager.set_interval(timer_id, 3000)
from pyloid.timer import PyloidTimerfrom pyloid import Pyloid# Create a PyloidTimer instanceapp =Pyloid(app_name="Pyloid-App")timer_manager =PyloidTimer()# Start a timertimer_id = timer_manager.start_periodic_timer(1000, lambda: print("Hello!"))# Change the timer interval to 3 secondstimer_manager.set_interval(timer_id, 3000)app.run()
Precise Timers
For more precise timing, you can use precise timers:
defprecise_task():print("Executing precise task")# Start a precise periodic timer with 100ms intervalprecise_timer_id = timer_manager.start_precise_periodic_timer(100, precise_task)
from pyloid.timer import PyloidTimerfrom pyloid import Pyloid# Create a PyloidTimer instanceapp =Pyloid(app_name="Pyloid-App")timer_manager =PyloidTimer()defprecise_task():print("Executing precise task")# Start a precise periodic timer with 100ms intervalprecise_timer_id = timer_manager.start_precise_periodic_timer(100, precise_task)app.run()
PyloidTimer is a powerful tool for managing various timing requirements easily. It can be useful in a wide range of situations, from simple periodic tasks to tasks that require precise timing.