Hi all,
I am learning to create an Interruptible routine for my camera sensor. My goal is to run an image acquisition routine on a remote process so that the local process can receive command from the user to perform other tasks. I start with this simple example. The routine creates a file, opens it and writes something into it at a certain period as shown below (Please note that the code in the while loop can be anything, I picked writing to a file as an example)
@everywhere function worker_func(fname::String, path::String)
Base.exit_on_sigint(false)
cd(path)
touch(fname)
f = open(fname,"w")
counter = 1
try
while true
if counter%1e8 == 0
# @info "."
# println(".")
#sleep(0.1)
write(f,"markerd\n")
end
counter += 1
end
catch e
if e isa InterruptException
# cleanup
println("function terminated by user")
write(f,"....... file is closed here ........")
close(f)
else
rethrow(e)
end
end
end
As seen in the code block, I comment out the stdout print functions (@info
and println
) and a sleep function to see its behavior first. I simply call the function in the local process - worker_func("output.txt", pwd())
. Here are the results
-
When the
@info
,println
, andsleep
are commented out, the output file has nothing in it. The keyboard interrupt signal is not received. The only way to stop the routine is send multiple SIGINT until the process throwsWARNING: Force throwing a SIGINT
and quits. -
When I uncomment either
@info
,println
, orsleep
, keyboard interrupt is easily received by the routine and it stops nicely. The output file also has texts in it with the expected manner.
Another experiment is I run this routine on a remote process. I call addprocs(3)
and then run the routine
remote_do(worker_func,2,fname,path)
I leave it running for a couple of tens of seconds before interrupting it by
interrupt(2)
And this is printed out
Worker 2 terminated.
However, the output file is empty.
Can anyone explain these behaviors? and suggest the best practice to interrupt a remote process?
Best,
Sitthichat