julia - 在 Julia 中编写模块 finalize 方法的正确方法是什么?

标签 julia

我正在努力寻找正确的使用方式终结者 在 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函数仅适用于用户定义和复合类型。它不适用于 ModuleFunction .

    更新:我记得 workspace重写以前的Main模块到 LastMain .因此,即使我们的模块无法从 Main 访问它在里面还活着LastMain范围(同样适用于我上面使用的功能)。

    关于julia - 在 Julia 中编写模块 finalize 方法的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33326779/

    相关文章:

    dataframe - Julia 中的 R 表函数(用于数据帧)

    module - @everywhere 的评估上下文

    asynchronous - 在 Julia 中使用 @sync @async 的简单并行性

    julia - Julia 多重调度入门

    io - 如何检查 Julia 中的文件是否为空?

    c - Julia:Ptr{Void} 终结器错误

    csv - 在 Julia 中读取 .dat 文件,可变分隔符间距的问题

    macros - Julia 中基准和时间宏的区别

    julia - 为什么我在 Juno 工作区中看不到任何变量?

    string - 在 Julia 中将 Int 转换为 String 的最佳方法是什么?