Simple loop frequency regulators in Python with an API similar to rospy.Rate
:
from loop_rate_limiters import RateLimiter
from time import perf_counter
rate = RateLimiter(frequency=400.0)
while True:
print(f"Hello from loop at {perf_counter():.3f} s")
rate.sleep()
A similar AsyncRateLimiter
class is available for asynchronous code.
conda install -c conda-forge loop-rate-limiters
pip install loop-rate-limiters
The AsyncRateLimiter
class provides a loop frequency limiter for asyncio:
import asyncio
from loop_rate_limiters import AsyncRateLimiter
async def main():
rate = AsyncRateLimiter(frequency=400.0)
while True:
loop_time = asyncio.get_event_loop().time()
print(f"Hello from loop at {loop_time:.3f} s")
await rate.sleep()
asyncio.run(main())
This can be used if, for instance, there are several tasks executed in parallel at different frequencies.
- ischedule: single-thread interval scheduler in Python