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-limiterspip install loop-rate-limitersWhile the example above is synchronous, this library also provides an AsyncRateLimiter class 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 when there are several tasks executed in parallel at different frequencies.
- ischedule: single-thread interval scheduler in Python