@contextmanager
def track_memory_usage(metric, course_id):
 memory_types = ['rss', 'vms']
 process = psutil.Process()
 baseline_memory_info = process.get_memory_info()
 baseline_usages = [getattr(baseline_memory_info, memory_type) for memory_type in memory_types]
 (yield)
 for (memory_type, baseline_usage) in zip(memory_types, baseline_usages):
  total_memory_info = process.get_memory_info()
  total_usage = getattr(total_memory_info, memory_type)
  memory_used = (total_usage - baseline_usage)
  dog_stats_api.increment(((metric + '.') + memory_type), memory_used, tags=['course_id:{}'.format(course_id)])

@ contextmanager def track_memory_usage ( metric , course_id ) : memory_types = [ 'rss' , 'vms' ] process = psutil . Process ( ) baseline_memory_info = process . get_memory_info ( ) baseline_usages = [ getattr ( baseline_memory_info , memory_type ) for memory_type in memory_types ] ( yield ) for ( memory_type , baseline_usage ) in zip ( memory_types , baseline_usages ) : total_memory_info = process . get_memory_info ( ) total_usage = getattr ( total_memory_info , memory_type ) memory_used = ( total_usage - baseline_usage ) dog_stats_api . increment ( ( ( metric + '.' ) + memory_type ) , memory_used , tags = [ 'course_id:{}' . format ( course_id ) ] )
context manager to track how much memory a given process uses .