Created
August 14, 2017 07:57
-
-
Save rafaelcaricio/b8fa59843e334d2fd3d5ad19d357afca to your computer and use it in GitHub Desktop.
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
import logging | |
import signal | |
import sys | |
import time | |
import tracemalloc | |
logger = logging.getLogger(__name__) | |
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) | |
tracemalloc.start() | |
MEM_SNAPSHOTS = [] | |
def take_snapshot(signum, frame): | |
global MEM_SNAPSHOTS | |
logger.debug('Got SIGUSR1 signal..') | |
MEM_SNAPSHOTS.append(tracemalloc.take_snapshot()) | |
# print stats if we have more than one snapshot | |
size = len(MEM_SNAPSHOTS) | |
if size > 1: | |
snapshot1, snapshot2 = MEM_SNAPSHOTS[size - 2], MEM_SNAPSHOTS[size - 1] | |
top_stats = snapshot2.compare_to(snapshot1, 'lineno') | |
output = "[ Top 10 differences ]\n" | |
for stat in top_stats[:10]: | |
output = '{}\n{}'.format(output, str(stat)) | |
logger.debug(output) | |
signal.signal(signal.SIGUSR1, take_snapshot) | |
count = 0 | |
while True: | |
print('Czesc {} raz.'.format(count)) | |
count = count + 1 | |
time.sleep(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment