Assume I have a module SomeModule.jl:
module SomeModule
export use_me
function use_me()
println("I'm useful!")
end
function call_me_in_the_end()
println("This is the end")
end
atexit(call_me_in_the_end)
end
And main.jl using this module:
import SomeModule: use_me
use_me()
Since function passed into atexit is called at process exit I expect to see when calling main.jl:
I'm useful!
This is the end
But instead I'm getting:
This is the end
I'm useful!
on the first call, and only:
I'm useful!
for all subsequent calls, until I make changes in SomeModule.jl. After changes are made everything repeats.
I've tried to bring atexit outside of the module, like so:
module SomeModule
...
end
import SomeModule: call_me_in_the_end
atexit(call_me_in_the_end)
but result is the same.
So, two questions:
- Why
call_me_in_the_endis called only once and how to fix it? - How to make
call_me_in_the_endbe called in the end?
Julia's version is 1.7.3 for macOS x86
The module gets compiled after the first run and hence the exit hook does not get attached in subsequent runs.
Instead you should have used the special
__init__that gets executed whenever module gets loaded:Regarding the output order this is just related to stream buffering and such behavior is observed quite often in multitask application. Simply add
flush(stdout)after your lastprintlnstatement in your code beforeexit()is called and you will be good.