@@ -14,8 +14,8 @@ namespace Microsoft.ML.AutoML
14
14
{
15
15
public class NotebookMonitor : IMonitor
16
16
{
17
+ private readonly ActionThrottler _updateThrottler ;
17
18
private DisplayedValue _valueToUpdate ;
18
- private DateTime _lastUpdate = DateTime . MinValue ;
19
19
20
20
public TrialResult BestTrial { get ; set ; }
21
21
public TrialResult MostRecentTrial { get ; set ; }
@@ -27,12 +27,14 @@ public NotebookMonitor()
27
27
{
28
28
CompletedTrials = new List < TrialResult > ( ) ;
29
29
TrialData = new DataFrame ( new PrimitiveDataFrameColumn < int > ( "Trial" ) , new PrimitiveDataFrameColumn < float > ( "Metric" ) , new StringDataFrameColumn ( "Trainer" ) , new StringDataFrameColumn ( "Parameters" ) ) ;
30
+ _updateThrottler = new ActionThrottler ( Update , TimeSpan . FromSeconds ( 5 ) ) ;
30
31
}
31
32
32
33
public void ReportBestTrial ( TrialResult result )
33
34
{
34
35
BestTrial = result ;
35
- Update ( ) ;
36
+
37
+ ThrottledUpdate ( ) ;
36
38
}
37
39
38
40
public void ReportCompletedTrial ( TrialResult result )
@@ -49,46 +51,36 @@ public void ReportCompletedTrial(TrialResult result)
49
51
new KeyValuePair < string , object > ( "Trainer" , result . TrialSettings . Pipeline . ToString ( ) . Replace ( "Unknown=>" , "" ) ) ,
50
52
new KeyValuePair < string , object > ( "Parameters" , activeRunParam ) ,
51
53
} , true ) ;
52
- Update ( ) ;
54
+
55
+ ThrottledUpdate ( ) ;
53
56
}
54
57
55
58
public void ReportFailTrial ( TrialResult result )
56
59
{
57
60
// TODO figure out what to do with failed trials.
58
- Update ( ) ;
61
+ ThrottledUpdate ( ) ;
59
62
}
60
63
61
64
public void ReportRunningTrial ( TrialSettings setting )
62
65
{
63
66
ActiveTrial = setting ;
64
- Update ( ) ;
67
+ ThrottledUpdate ( ) ;
65
68
}
66
69
67
- private int _updatePending = 0 ;
68
- public void Update ( )
70
+ private void ThrottledUpdate ( )
69
71
{
70
- Task . Run ( async ( ) =>
71
- {
72
- if ( Interlocked . CompareExchange ( ref _updatePending , 1 , 0 ) == 0 ) // _updatePending is int initialized with 0
73
- {
74
- DateTime n = DateTime . UtcNow ;
75
- if ( n - _lastUpdate < TimeSpan . FromSeconds ( 5 ) )
76
- {
77
- await Task . Delay ( n - _lastUpdate ) ;
78
- }
79
-
80
- _valueToUpdate . Update ( this ) ;
81
- _lastUpdate = n ;
82
- _updatePending = 0 ;
83
- }
84
- } ) ;
72
+ Task . Run ( async ( ) => await _updateThrottler . ExecuteAsync ( ) ) ;
73
+ }
85
74
75
+ public void Update ( )
76
+ {
77
+ _valueToUpdate . Update ( this ) ;
86
78
}
87
79
88
80
public void SetUpdate ( DisplayedValue valueToUpdate )
89
81
{
90
82
_valueToUpdate = valueToUpdate ;
91
- Update ( ) ;
83
+ ThrottledUpdate ( ) ;
92
84
}
93
85
}
94
86
}
0 commit comments