Couldn't you use InterruptExceptions here (ctrl-c handlers), disabling
sigint during the loop body so that the exception only occurs when you are
ready?
for i in iterations
try
disable_sigint() do
....do an iteration....
end
catch e
if isa(e, InterruptException)
...exit gracefully....
else
rethrow(e)
end
end
end
? This kind of thing has a chance to work in an environment like IJulia
too, whereas reading stdin will never work in a browser interface.
Unfortunately, the above code may still have a race condition: if the user
hits ctrl-c and the SIGINT signal is received in the loop body but before
try...end then it won't be caught by the catch bock. One way to hack
around this would be to put the disable_sigint() outside the loop, and then
check the C global jl_signal_pending != 0 inside the loop body to see if a
SIGINT was received (but not handled yet).