1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
# Throttler
[]()
[](https://pypi.python.org/pypi/throttler)
[](https://github.com/uburuntu/throttler/blob/master/LICENSE)
[](https://github.com/uburuntu/throttler/actions/workflows/tests.yml)
[](https://codecov.io/gh/uburuntu/throttler)
Zero-dependency Python package for easy throttling with asyncio support.
> 
## 📝 Table of Contents
- 🎒 [Install](#-install)
- 🛠 [Usage Examples](#-usage-examples)
- [Throttler and ThrottlerSimultaneous](#throttler-and-throttlersimultaneous)
- [Simple Example](#simple-example)
- [API Example](#api-example)
- [ExecutionTimer](#executiontimer)
- [Timer](#timer)
- 👨🏻💻 [Author](#-author)
- 💬 [Contributing](#-contributing)
- 📝 [License](#-license)
## 🎒 Install
Just
```sh
pip install throttler
```
## 🛠 Usage Examples
All run-ready examples are [here](examples).
### Throttler and ThrottlerSimultaneous
**Throttler**:
> Context manager for limiting rate of accessing to context block.
```python
from throttler import Throttler
# Limit to three calls per second
t = Throttler(rate_limit=3, period=1.0)
async with t:
pass
```
Or
```python
import asyncio
from throttler import throttle
# Limit to three calls per second
@throttle(rate_limit=3, period=1.0)
async def task():
return await asyncio.sleep(0.1)
```
**ThrottlerSimultaneous**:
> Context manager for limiting simultaneous count of accessing to context block.
```python
from throttler import ThrottlerSimultaneous
# Limit to five simultaneous calls
t = ThrottlerSimultaneous(count=5)
async with t:
pass
```
Or
```python
import asyncio
from throttler import throttle_simultaneous
# Limit to five simultaneous calls
@throttle_simultaneous(count=5)
async def task():
return await asyncio.sleep(0.1)
```
#### Simple Example
```python
import asyncio
import time
from throttler import throttle
# Limit to two calls per second
@throttle(rate_limit=2, period=1.0)
async def task():
return await asyncio.sleep(0.1)
async def many_tasks(count: int):
coros = [task() for _ in range(count)]
for coro in asyncio.as_completed(coros):
_ = await coro
print(f'Timestamp: {time.time()}')
asyncio.run(many_tasks(10))
```
Result output:
```text
Timestamp: 1585183394.8141203
Timestamp: 1585183394.8141203
Timestamp: 1585183395.830335
Timestamp: 1585183395.830335
Timestamp: 1585183396.8460555
Timestamp: 1585183396.8460555
...
```
#### API Example
```python
import asyncio
import time
import aiohttp
from throttler import Throttler, ThrottlerSimultaneous
class SomeAPI:
api_url = 'https://example.com'
def __init__(self, throttler):
self.throttler = throttler
async def request(self, session: aiohttp.ClientSession):
async with self.throttler:
async with session.get(self.api_url) as resp:
return resp
async def many_requests(self, count: int):
async with aiohttp.ClientSession() as session:
coros = [self.request(session) for _ in range(count)]
for coro in asyncio.as_completed(coros):
response = await coro
print(f'{int(time.time())} | Result: {response.status}')
async def run():
# Throttler can be of any type
t = ThrottlerSimultaneous(count=5) # Five simultaneous requests
t = Throttler(rate_limit=10, period=3.0) # Ten requests in three seconds
api = SomeAPI(t)
await api.many_requests(100)
asyncio.run(run())
```
Result output:
```text
1585182908 | Result: 200
1585182908 | Result: 200
1585182908 | Result: 200
1585182909 | Result: 200
1585182909 | Result: 200
1585182909 | Result: 200
1585182910 | Result: 200
1585182910 | Result: 200
1585182910 | Result: 200
...
```
### ExecutionTimer
> Context manager for time limiting of accessing to context block. Simply sleep `period` secs before next accessing, not analog of `Throttler`. Also it can align to start of minutes.
```python
import time
from throttler import ExecutionTimer
et = ExecutionTimer(60, align_sleep=True)
while True:
with et:
print(time.asctime(), '|', time.time())
```
Or
```python
import time
from throttler import execution_timer
@execution_timer(60, align_sleep=True)
def f():
print(time.asctime(), '|', time.time())
while True:
f()
```
Result output:
```text
Thu Mar 26 00:56:17 2020 | 1585173377.1203406
Thu Mar 26 00:57:00 2020 | 1585173420.0006166
Thu Mar 26 00:58:00 2020 | 1585173480.002517
Thu Mar 26 00:59:00 2020 | 1585173540.001494
```
### Timer
> Context manager for pretty printing start, end, elapsed and average times.
```python
import random
import time
from throttler import Timer
timer = Timer('My Timer', verbose=True)
for _ in range(3):
with timer:
time.sleep(random.random())
```
Or
```python
import random
import time
from throttler import timer
@timer('My Timer', verbose=True)
def f():
time.sleep(random.random())
for _ in range(3):
f()
```
Result output:
```text
#1 | My Timer | begin: 2020-03-26 01:46:07.648661
#1 | My Timer | end: 2020-03-26 01:46:08.382135, elapsed: 0.73 sec, average: 0.73 sec
#2 | My Timer | begin: 2020-03-26 01:46:08.382135
#2 | My Timer | end: 2020-03-26 01:46:08.599919, elapsed: 0.22 sec, average: 0.48 sec
#3 | My Timer | begin: 2020-03-26 01:46:08.599919
#3 | My Timer | end: 2020-03-26 01:46:09.083370, elapsed: 0.48 sec, average: 0.48 sec
```
## 👨🏻💻 Author
**Ramzan Bekbulatov**:
- Telegram: [@rm_bk](https://t.me/rm_bk)
- Github: [@uburuntu](https://github.com/uburuntu)
## 💬 Contributing
Contributions, issues and feature requests are welcome!
## 📝 License
This project is [MIT](https://github.com/uburuntu/throttler/blob/master/LICENSE) licensed.
|