Loop Rate Limiters

1.2.0 · active · verified Thu Apr 16

Loop-rate-limiters (current version 1.2.0) is a Python library providing simple frequency regulators for loops, offering an API similar to `rospy.Rate`. It supports both synchronous and asynchronous (asyncio) operations, ensuring code execution at a desired frequency. The library maintains an active development pace with regular patch and minor releases to introduce new features, fix bugs, and improve logging.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the usage of `AsyncRateLimiter` to regulate the frequency of an asynchronous loop. A synchronous `RateLimiter` example is also included to show its basic usage.

import asyncio
from loop_rate_limiters import AsyncRateLimiter
import os

async def main():
    # Example with an asynchronous rate limiter
    # Limits to 400 Hz (iterations per second)
    rate = AsyncRateLimiter(frequency=400.0)
    for i in range(10):
        loop_time = asyncio.get_event_loop().time()
        print(f"Async loop iteration {i} at {loop_time:.3f} s")
        await rate.sleep()

    print("\nSynchronous example (not shown in quickstart, but available):")
    from time import perf_counter
    from loop_rate_limiters import RateLimiter
    sync_rate = RateLimiter(frequency=10.0) # 10 Hz
    for i in range(5):
        print(f"Sync loop iteration {i} at {perf_counter():.3f} s")
        sync_rate.sleep()

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →