Skip to content

Commit

Permalink
fix chunking
Browse files Browse the repository at this point in the history
  • Loading branch information
tilsche committed Aug 21, 2023
1 parent 1b683e2 commit 71fe55b
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions metricq_source_snmp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,10 @@
from .version import __version__ # noqa: F401 # magic import for automatic version


# Patiently waiting for Python 3.12 to import this diretly
def batched(iterable, n):
# batched('ABCDEFG', 3) --> ABC DEF G
if n < 1:
raise ValueError("n must be at least one")
it = iter(iterable)
while batch := tuple(islice(it, n)):
yield batch
# https://stackoverflow.com/a/2135920
def split(a, n):
k, m = divmod(len(a), n)
return (a[i * k + min(i, m) : (i + 1) * k + min(i + 1, m)] for i in range(n))


NaN = float("nan")
Expand Down Expand Up @@ -307,7 +303,7 @@ async def _on_config(
)

# distribute host configurations to workers
for chunk in batched(objects_by_host.keys(), num_procs):
for chunk in split(objects_by_host.keys(), num_procs):
self.workers.apply_async(
mp_worker,
(
Expand Down

0 comments on commit 71fe55b

Please sign in to comment.