@@ -29,22 +29,22 @@ class Batcher(Generic[T]):
29
29
30
30
# Triggers when 2 (or more) items are added
31
31
batcher = Batcher(max_count=2)
32
- assert batcher.add(["foo ", "bar ", "baz "])
33
- assert batcher.flush() == ["foo ", "bar ", "baz "]
32
+ assert batcher.add(["item1 ", "item2 ", "item3 "])
33
+ assert batcher.flush() == ["item1 ", "item2 ", "item3 "]
34
34
35
35
# Triggers partially when 2 (or more) items are added
36
36
batcher = Batcher(max_count=2)
37
- assert batcher.add(["foo ", "bar ", "baz "])
38
- assert batcher.flush(partial=True) == ["foo ", "bar "]
39
- assert batcher.add("foobar ")
40
- assert batcher.flush(partial=True) == ["baz ", "foobar "]
37
+ assert batcher.add(["item1 ", "item2 ", "item3 "])
38
+ assert batcher.flush(partial=True) == ["item1 ", "item2 "]
39
+ assert batcher.add("item4 ")
40
+ assert batcher.flush(partial=True) == ["item3 ", "item4 "]
41
41
42
42
# Trigger 2 seconds after the first add
43
43
batcher = Batcher(max_window=2.0)
44
- assert not batcher.add(["foo ", "bar ", "baz "])
44
+ assert not batcher.add(["item1 ", "item2 ", "item3 "])
45
45
time.sleep(2.1)
46
- assert not batcher.add(["foobar "])
47
- assert batcher.flush() == ["foo ", "bar ", "baz ", "foobar "]
46
+ assert not batcher.add(["item4 "])
47
+ assert batcher.flush() == ["item1 ", "item2 ", "item3 ", "item4 "]
48
48
"""
49
49
50
50
max_count : Optional [int ] = Field (default = None , description = "Maximum number of items" , ge = 0 )
@@ -108,6 +108,7 @@ def flush(self, *, partial=False) -> list[T]:
108
108
109
109
self ._last_batch_time = time .monotonic ()
110
110
self ._triggered = False
111
+ self ._check_batch_policy ()
111
112
112
113
return result
113
114
0 commit comments