simplified rateLimiter
resists better to changing in/out conditions limits risks of "catching up"
This commit is contained in:
parent
e11f91b039
commit
09c9cf3f51
@ -19,30 +19,22 @@ import time
|
||||
|
||||
MB = 1024 * 1024
|
||||
rate = float(sys.argv[1]) * MB
|
||||
rate *= 1.4 # compensation for excluding i/o time (experimentally determined)
|
||||
start = time.time()
|
||||
total_read = 0
|
||||
|
||||
sys.stderr.close() # remove error message, for Ctrl+C
|
||||
# sys.stderr.close() # remove error message, for Ctrl+C
|
||||
|
||||
try:
|
||||
buf = " "
|
||||
while len(buf):
|
||||
now = time.time()
|
||||
to_read = max(int(rate * (now - start) - total_read), 1)
|
||||
to_read = max(int(rate * (now - start)), 1)
|
||||
max_buf_size = 1 * MB
|
||||
to_read = min(to_read, max_buf_size)
|
||||
start = now
|
||||
|
||||
read_start = time.time()
|
||||
buf = sys.stdin.buffer.read(to_read)
|
||||
|
||||
write_start = read_end = time.time()
|
||||
sys.stdout.buffer.write(buf)
|
||||
write_end = time.time()
|
||||
|
||||
wait_time = max(read_end - read_start, write_end - write_start)
|
||||
start += wait_time # exclude delay of the slowest
|
||||
total_read += len(buf)
|
||||
|
||||
except (KeyboardInterrupt, BrokenPipeError) as e:
|
||||
pass
|
||||
|
Loading…
Reference in New Issue
Block a user