This repository has been archived by the owner on Dec 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 335
Remove shared dicts #64
Closed
Closed
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import logging | ||
from Queue import Empty | ||
from redis import StrictRedis | ||
from time import time, sleep | ||
from threading import Thread | ||
from collections import defaultdict | ||
from multiprocessing import Process, Manager, Lock | ||
from multiprocessing import Process, Manager, Queue | ||
from msgpack import Unpacker, unpackb, packb | ||
from os import path, kill, getpid, system | ||
from math import ceil | ||
|
@@ -27,10 +28,12 @@ def __init__(self, parent_pid): | |
self.daemon = True | ||
self.parent_pid = parent_pid | ||
self.current_pid = getpid() | ||
self.lock = Lock() | ||
self.exceptions = Manager().dict() | ||
self.anomaly_breakdown = Manager().dict() | ||
self.exceptions = dict() | ||
self.anomaly_breakdown = dict() | ||
self.anomalous_metrics = Manager().list() | ||
self.exceptions_q = Queue() | ||
self.anomaly_breakdown_q = Queue() | ||
|
||
|
||
def check_if_parent_is_alive(self): | ||
""" | ||
|
@@ -108,20 +111,12 @@ def spin_process(self, i, unique_metrics): | |
exceptions['Other'] += 1 | ||
logger.info(traceback.format_exc()) | ||
|
||
# Collate process-specific dicts to main dicts | ||
with self.lock: | ||
for key, value in anomaly_breakdown.items(): | ||
if key not in self.anomaly_breakdown: | ||
self.anomaly_breakdown[key] = value | ||
else: | ||
self.anomaly_breakdown[key] += value | ||
|
||
for key, value in exceptions.items(): | ||
if key not in self.exceptions: | ||
self.exceptions[key] = value | ||
else: | ||
self.exceptions[key] += value | ||
# Add values to the queue so the parent process can collate | ||
for key, value in anomaly_breakdown.items(): | ||
self.anomaly_breakdown_q.put((key, value)) | ||
|
||
for key, value in exceptions.items(): | ||
self.exceptions_q.put((key, value)) | ||
|
||
|
||
def run(self): | ||
|
@@ -163,6 +158,28 @@ def run(self): | |
for p in pids: | ||
p.join() | ||
|
||
# Grab data from the queue and populate dictionaries | ||
while 1: | ||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we please do 4 spaces indentation as per PEP8? |
||
key, value = self.anomaly_breakdown_q.get_nowait() | ||
if key not in self.anomaly_breakdown.keys(): | ||
self.anomaly_breakdown[key] = value | ||
else: | ||
self.anomaly_breakdown[key] += value | ||
except Empty: | ||
break | ||
|
||
while 1: | ||
try: | ||
key, value = self.exceptions_q.get_nowait() | ||
if key not in self.exceptions.keys(): | ||
self.exceptions[key] = value | ||
else: | ||
self.exceptions[key] += value | ||
except Empty: | ||
break | ||
|
||
|
||
# Send alerts | ||
if settings.ENABLE_ALERTS: | ||
for alert in settings.ALERTS: | ||
|
@@ -218,8 +235,8 @@ def run(self): | |
|
||
# Reset counters | ||
self.anomalous_metrics[:] = [] | ||
self.exceptions = Manager().dict() | ||
self.anomaly_breakdown = Manager().dict() | ||
self.exceptions = dict() | ||
self.anomaly_breakdown = dict() | ||
|
||
# Sleep if it went too fast | ||
if time() - now < 5: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If these are only accessed during the run() method, might it better to remove them as class properties and treat them as local variables only (in the run() method)?