11import os
22import json
33import psutil
4- from traitlets import Float , Int , default
4+ from traitlets import Float , Int , Union , default
55from traitlets .config import Configurable
66from notebook .utils import url_path_join
77from notebook .base .handlers import IPythonHandler
88from tornado import web
9+ try :
10+ # Traitlets >= 4.3.3
11+ from traitlets import Callable
12+ except ImportError :
13+ from .callable import Callable
914
1015
1116class MetricsHandler (IPythonHandler ):
@@ -19,11 +24,16 @@ def get(self):
1924 all_processes = [cur_process ] + cur_process .children (recursive = True )
2025 rss = sum ([p .memory_info ().rss for p in all_processes ])
2126
27+ if callable (config .mem_limit ):
28+ mem_limit = config .mem_limit (rss = rss )
29+ else : # mem_limit is an Int
30+ mem_limit = config .mem_limit
31+
2232 limits = {}
2333
2434 if config .mem_limit != 0 :
2535 limits ['memory' ] = {
26- 'rss' : config . mem_limit
36+ 'rss' : mem_limit
2737 }
2838 if config .mem_warning_threshold != 0 :
2939 limits ['memory' ]['warn' ] = (config .mem_limit - rss ) < (config .mem_limit * config .mem_warning_threshold )
@@ -68,22 +78,22 @@ class ResourceUseDisplay(Configurable):
6878 we will start warning the user when they use (128 - (128 * 0.1)) MB.
6979
7080 Set to 0 to disable warning.
71- """ ,
72- config = True
73- )
81+ """
82+ ).tag (config = True )
7483
75- mem_limit = Int (
84+ mem_limit = Union (
85+ trait_types = [Int (), Callable ()],
7686 0 ,
77- config = True ,
7887 help = """
7988 Memory limit to display to the user, in bytes.
89+ Can also be a function which calculates the memory limit.
8090
8191 Note that this does not actually limit the user's memory usage!
8292
8393 Defaults to reading from the `MEM_LIMIT` environment variable. If
8494 set to 0, no memory limit is displayed.
8595 """
86- )
96+ ). tag ( config = True )
8797
8898 @default ('mem_limit' )
8999 def _mem_limit_default (self ):
0 commit comments