Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and use _chunker method when sending metrics #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions pyzabbix/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,27 @@ def _chunk_send(self, metrics):
raise socket.error(response)

return response

def _chunker(self, iterable):
"""Batch `iterable` into chunks of size `chunk_size`.

:type iterable: Iterable
:param iterable: any iterable to be chunked

:rtype: Iterable
:return: Chunk generator
"""
if self.chunk_size is None:
yield iterable
return
chunk = []
for item in iterable:
chunk.append(item)
if len(chunk) >= self.chunk_size:
yield chunk
chunk = []
if chunk:
yield chunk

def send(self, metrics):
"""Send the metrics to zabbix server.
Expand All @@ -439,6 +460,6 @@ def send(self, metrics):
:return: Parsed response from Zabbix Server
"""
result = ZabbixResponse()
for m in range(0, len(metrics), self.chunk_size):
result.parse(self._chunk_send(metrics[m:m + self.chunk_size]))
for chunk in self._chunker(metrics):
result.parse(self._chunk_send(chunk))
return result