Last active
October 31, 2016 21:31
-
-
Save zed/9cb41b2cfe615a7be3e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
"""Generate data at constant rate (float interval). | |
To try, run: | |
$ python3 constant-bit-rate-modulo.py | pv >/dev/null | |
""" | |
from time import sleep, monotonic as timer | |
chunksize, interval = 1024, 0.03125 # 32KiB/s | |
chunk = 'x' * chunksize | |
while True: | |
# assume print(chunk) takes less than interval seconds | |
sleep(interval - timer() % interval) # use modulo to support float interval | |
print(chunk) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
"""Generate data at constant rate. | |
To try, run: | |
$ python3 constant-bit-rate.py | pv >/dev/null | |
""" | |
from time import sleep, monotonic as timer | |
chunksize = 1 << 15 # 32K | |
interval = 1 # per second | |
chunk = 'x' * chunksize | |
deadline = timer() | |
while True: | |
deadline += interval | |
print(chunk) | |
delay = deadline - timer() | |
if delay > 0: | |
sleep(delay) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment