Loop rate limiters

Build Documentation Coverage Conda version PyPI version

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.

Installation

You can install the library straight from PyPI:

pip install loop-rate-limiters

Or equivalently from Conda:

conda install -c conda-forge loop-rate-limiters

Asynchronous I/O

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.

See also

  • ischedule: single-thread interval scheduler in Python