-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix logging, concurrency and interruption (#769)
* ensure logging and threading * move generate load to sender
- Loading branch information
Showing
6 changed files
with
42 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,43 @@ | ||
import logging | ||
import threading | ||
from concurrent.futures import ThreadPoolExecutor | ||
from itertools import islice | ||
from typing import Iterable | ||
|
||
from logprep.abc.output import Output | ||
|
||
logger = logging.getLogger("Sender") | ||
|
||
|
||
class Sender: | ||
"""Manages the Batcher and Output classes""" | ||
|
||
def __init__(self, input_events: Iterable, output: Output, **config): | ||
self.config = config | ||
self.output = output | ||
self.thread_count = config.get("thread_count", 1) | ||
self.input_events = iter(input_events) | ||
self.target_url = config.get("target_url") | ||
self._lock = threading.Lock() | ||
self.exit_requested = False | ||
if not self.target_url: | ||
raise ValueError("No target_url specified") | ||
|
||
def send_batch(self) -> None: | ||
def send_batches(self) -> None: | ||
"""Loads a batch from the message backlog and sends to the endpoint""" | ||
target_url = self.target_url | ||
while self.input_events: | ||
with self._lock: | ||
batch = next(self.input_events) | ||
# line starts with the target path of the target url | ||
self.output.store(f"{target_url}{batch}") | ||
event_generator = (f"{target_url}{batch}" for batch in self.input_events) | ||
with ThreadPoolExecutor(max_workers=self.thread_count) as executor: | ||
while not self.exit_requested: | ||
chunk = tuple(islice(event_generator, self.thread_count)) | ||
if not chunk: | ||
break | ||
for _ in executor.map(self.output.store, chunk): | ||
logger.debug( | ||
"During generate load active threads: %s", threading.active_count() | ||
) | ||
logger.debug("After generate load active threads: %s", threading.active_count()) | ||
|
||
def stop(self) -> None: | ||
"""Stops the sender""" | ||
self.exit_requested = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters