Skip to content

Instantly share code, notes, and snippets.

@rafaelcaricio
Created August 14, 2017 07:57
Show Gist options
  • Save rafaelcaricio/b8fa59843e334d2fd3d5ad19d357afca to your computer and use it in GitHub Desktop.
Save rafaelcaricio/b8fa59843e334d2fd3d5ad19d357afca to your computer and use it in GitHub Desktop.
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