我正在努力寻找正确的使用方式终结者 在 Julia
请参阅 Julia 文档:
finalizer(x, function)
Register a function f(x) to be called when there are no program-accessible references to x. The behavior of this function is unpredictable if x is of a bits type.
首先,我使用 TestModule.jl 生成了一个 TestModule 标准包
#in TestModule.jl
module TestModule
end
finalizer(TestModule,(t)->println("fin"))
还有一个runtest.jl#in runtest.jl
using Base.Test
using TestModule
然后我尝试测试包,但在测试通过时收到错误:julia> Pkg.test("TestModule")
INFO: Testing TestModule
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
INFO: TestModule tests passed
之后我安排了另一个测试用例julia> workspace() # new workspace
julia> typeof(TestModule) # make sure *there are no program-accessible references to `TestModule`*
ERROR: UndefVarError: TestModule not defined
julia> using TestModule
julia> finalize(TestModule)
fin # finalize method is working
julia> typeof(TestModule)
Module # make sure *there is program-accessible reference to `TestModule`*
julia> workspace() # force clear references
julia> typeof(TestModule) # check that *there are no program-accessible references*
ERROR: UndefVarError: TestModule not defined
根据上述测试用例我有一些问题finalize
TestModule
的方法在测试过程中产生错误?finalize
清除引用时未调用方法finalize
的正确方法是什么模块的方法(操作系统=Ubuntu, Julia 版本=0.4.0)
编辑
正如@Maciek 所提到的,调用
gc()
在 workspace()
之后还有,不要帮忙。谢谢
最佳答案
恕我直言,workspace
没有俘虏,此外finalizer
仅适用于用户定义和复合类型。
我已经进行了一些测试。看看我的结果:
julia> type Foo
x
Foo(x) = begin obj = new(x); finalizer(obj,(o) -> println("The end.")); return obj end
end
julia> Foo(1)
julia> workspace()
julia> gc()
Module the end.error in running finalizer: ErrorException("task switch not allowed from inside gc finalizer")
The end.error in running finalizer: ErrorException("task switch not allowed from inside gc finalizer")
另一个在模块范围内定义对象的测试:
julia> module FinMod
type T
x::Int
end
finalizer(T(1), (t) -> println("Module the end."))
end
FinMod
julia> FinMod
FinMod
julia> workspace()
julia> gc()
Module the end.error in running finalizer: ErrorException("task switch not allowed from inside gc finalizer")
函数(一流的对象)呢?
julia> function foo() println("I'm foo") end
foo (generic function with 1 method)
julia> finalizer(foo, (f) -> println("foo function is dead now."))
julia> foo
foo (generic function with 1 method)
julia> workspace()
julia> foo
ERROR: UndefVarError: foo not defined
julia> gc()
julia> #nothing happened
所以,总结一下:在我看来
workspace
不打电话 finalize
. finalizer
函数仅适用于用户定义和复合类型。它不适用于 Module
或 Function
.更新:我记得
workspace
重写以前的Main
模块到 LastMain
.因此,即使我们的模块无法从 Main
访问它在里面还活着LastMain
范围(同样适用于我上面使用的功能)。
关于julia - 在 Julia 中编写模块 finalize 方法的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33326779/