mirror of
https://github.com/simon987/hexlib.git
synced 2025-04-10 14:06:43 +00:00
Add buffered decorator
This commit is contained in:
parent
7506ddaf9a
commit
3adca8cb5e
@ -1,5 +1,6 @@
|
||||
import time
|
||||
|
||||
import atexit
|
||||
import siphash
|
||||
|
||||
last_time_called = dict()
|
||||
@ -25,6 +26,27 @@ def rate_limit(per_second):
|
||||
return decorate
|
||||
|
||||
|
||||
def buffered(batch_size: int, flush_on_exit: bool = False):
|
||||
|
||||
def decorate(func):
|
||||
buffer = []
|
||||
if flush_on_exit:
|
||||
atexit.register(func, buffer)
|
||||
|
||||
def wrapper(*items):
|
||||
|
||||
for item in items:
|
||||
buffer.append(item)
|
||||
|
||||
if len(buffer) >= batch_size:
|
||||
func(buffer)
|
||||
buffer.clear()
|
||||
|
||||
return wrapper
|
||||
|
||||
return decorate
|
||||
|
||||
|
||||
Key = b"0123456789ABCDEF"
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user