Julia有一个Timer对象,它可以以设定的重复率运行回调函数。根据{{3}},使用Timer的唯一函数是start_timer()和stop_timer()。
给定一个Timer,有没有办法检查它当前是否正在运行?
答案 0 :(得分:4)
查找类似内容的最佳方式是methodswith
。不幸的是,没有为Julia Timer
对象定义多种方法:
julia> methodswith(Timer, true) # true to check super types, too (but not Any)
5-element Array{Method,1}:
stop_timer(timer::Timer) at stream.jl:499
close(t::Timer) at stream.jl:460
start_timer(timer::Timer,timeout::Int64,repeat::Int64) at deprecated.jl:204
start_timer(timer::Timer,timeout::Real,repeat::Real) at stream.jl:490
close(t::Timer) at stream.jl:460
所以我们必须深入挖掘一下。查看implementation for Timer表明它只是包装了一个libuv计时器对象。所以我只是通过libuv/include/uv.h搜索了计时器API,找到了int uv_is_active(const uv_handle_t* handle)
,看起来非常有前景。我只是将这个c调用包装在Julian函数中,它就像一个魅力:
julia> isactive(t::Timer) = bool(ccall(:uv_is_active, Cint, (Ptr{Void},), t.handle));
julia> t = Timer((x)->println(STDOUT,"\nboo"));
julia> isactive(t)
false
julia> start_timer(t, 10., 0); # fire in 10 seconds, don't repeat
julia> isactive(t)
true
julia>
boo
julia> isactive(t)
false