1
1
using ProgressMeter
2
+ using TaskLocalValues
2
3
3
4
struct MonitorTaskQueue <: AbstractTaskQueue
4
- runningTasks:: Vector{Pair{EagerTaskSpec,EagerThunk}}
5
- finishedTasks:: Vector{Pair{EagerTaskSpec,EagerThunk}}
6
- MonitorTaskQueue () = new (Pair{EagerTaskSpec,EagerThunk}[], Pair{EagerTaskSpec,EagerThunk}[])
5
+ running_tasks:: Vector{WeakRef}
6
+ MonitorTaskQueue () = new (WeakRef[])
7
7
end
8
8
function enqueue! (queue:: MonitorTaskQueue , spec:: Pair{EagerTaskSpec,EagerThunk} )
9
- push! (queue. runningTasks, spec)
9
+ push! (queue. running_tasks, WeakRef ( spec[ 2 ]) )
10
10
upper = get_options (:task_queue , EagerTaskQueue ())
11
- enqueue! (upper,spec)
11
+ enqueue! (upper, spec)
12
12
end
13
13
14
14
function enqueue! (queue:: MonitorTaskQueue , specs:: Vector{Pair{EagerTaskSpec,EagerThunk}} )
15
- append! (queue. runningTasks, specs)
15
+ for (_, task) in specs
16
+ push! (queue. running_tasks, WeakRef (task))
17
+ end
18
+ upper = get_options (:task_queue , EagerTaskQueue ())
19
+ enqueue! (upper, specs)
16
20
end
17
21
18
- const MONITOR_QUEUE = MonitorTaskQueue ( )
22
+ const MONITOR_QUEUE = TaskLocalValue { MonitorTaskQueue} (MonitorTaskQueue )
19
23
24
+ " Monitors and displays the progress of any still-executing tasks."
20
25
function monitor ()
21
- errormonitor (Threads. @spawn begin
22
- runningTasks = MONITOR_QUEUE. runningTasks
23
- finishedTasks = MONITOR_QUEUE. finishedTasks
24
- meter = Progress (length (runningTasks))
25
- while ! isempty (runningTasks)
26
- for (i,task) in reverse (collect (enumerate (runningTasks)))
27
- if isready (task[2 ])
28
- next! (meter)
29
- push! (finishedTasks,task)
30
- deleteat! (runningTasks,i)
31
- end
32
- end
33
- sleep (0.1 )
26
+ queue = MONITOR_QUEUE[]
27
+ running_tasks = queue. running_tasks
28
+ isempty (running_tasks) && return
29
+
30
+ ntasks = length (running_tasks)
31
+ meter = Progress (ntasks;
32
+ desc= " Waiting for $ntasks tasks..." ,
33
+ dt= 0.01 , showspeed= true )
34
+ while ! isempty (running_tasks)
35
+ for (i, task_weak) in reverse (collect (enumerate (running_tasks)))
36
+ task = task_weak. value
37
+ if task === nothing || isready (task)
38
+ next! (meter)
39
+ deleteat! (running_tasks, i)
34
40
end
35
- end )
36
- end
41
+ end
42
+ sleep (0.01 )
43
+ end
44
+ finish! (meter)
45
+
46
+ return
47
+ end
0 commit comments