Add buffered decorator

This commit is contained in:
simon 2019-12-19 08:55:30 -05:00
parent 7506ddaf9a
commit 3adca8cb5e
2 changed files with 23 additions and 1 deletions

View File

@ -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"

View File

@ -2,7 +2,7 @@ from setuptools import setup
setup(
name="hexlib",
version="1.2",
version="1.3",
description="Misc utility methods",
author="simon987",
author_email="me@simon987.net",