Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/upkie/loop-rate-limiters
Loop rate limiters in Python with an API similar to rospy.Rate
https://github.com/upkie/loop-rate-limiters
python rate-limiting robotics rospy
Last synced: 2 months ago
JSON representation
Loop rate limiters in Python with an API similar to rospy.Rate
- Host: GitHub
- URL: https://github.com/upkie/loop-rate-limiters
- Owner: upkie
- License: apache-2.0
- Created: 2022-12-02T14:27:06.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-29T06:59:11.000Z (4 months ago)
- Last Synced: 2024-11-01T18:16:19.749Z (2 months ago)
- Topics: python, rate-limiting, robotics, rospy
- Language: Python
- Homepage:
- Size: 4.08 MB
- Stars: 25
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Loop rate limiters
[![Build](https://img.shields.io/github/actions/workflow/status/upkie/loop-rate-limiters/main.yml?branch=main)](https://github.com/upkie/loop-rate-limiters/actions)
[![Documentation](https://img.shields.io/github/actions/workflow/status/upkie/loop-rate-limiters/docs.yml?branch=main&label=docs)](https://upkie.github.io/loop-rate-limiters/)
[![Coverage](https://coveralls.io/repos/github/upkie/loop-rate-limiters/badge.svg?branch=main)](https://coveralls.io/github/upkie/loop-rate-limiters?branch=main)
[![Conda version](https://img.shields.io/conda/vn/conda-forge/loop-rate-limiters.svg)](https://anaconda.org/conda-forge/loop-rate-limiters)
[![PyPI version](https://img.shields.io/pypi/v/loop-rate-limiters)](https://pypi.org/project/loop-rate-limiters/)Simple loop frequency regulators in Python with an API similar to ``rospy.Rate``:
```python
from loop_rate_limiters import RateLimiter
from time import perf_counterrate = 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](https://github.com/upkie/loop-rate-limiters#asynchronous-io).
## Installation
### From conda-forge
```console
conda install -c conda-forge loop-rate-limiters
```### From PyPI
```console
pip install loop-rate-limiters
```## Asynchronous I/O
The ``AsyncRateLimiter`` class provides a loop frequency limiter for [asyncio](https://docs.python.org/3/library/asyncio.html):
```python
import asyncio
from loop_rate_limiters import AsyncRateLimiterasync 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](https://github.com/aleksve/ischedule): single-thread interval scheduler in Python