Interrupts in @async code

ettersi
Fri, 10 Mar 2023 03:47:06 +0000
General Usage

I’m trying to wrap my head around how Julia handles interrupts in @async code. I have three different scripts that I’m trying to interrupt using ctrl+c, with various degrees of success.

Script1:

julia> @async begin
           for _ = 1:10
               println("Hey apple!")
               sleep(0.5)
           end
       end

This cannot be interrupted, instead it prints “Hey apple” ten times regardless of how many times I hit ctrl+c.

Script 2

julia> @sync begin
           @async begin
               for _ = 1:10
                   println("Hey apple!")
                   sleep(0.5)
               end
           end
       end

This terminates the first time I hit ctrl+c.

Script 3

julia> @sync begin
           @async begin
               for _ = 1:10
                   println("Hey apple!")
                   sleep(0.5)
               end
           end
           @async begin
               for _ = 1:10
                   println("What?")
                   sleep(1.0)
               end
           end
       end

Hitting ctrl+c on this terminates the “Hey apple!” task, but doesn’t stop the “What?” task.

The behaviour of scripts 1 and 2 seems somewhat sensible, but the behaviour of script 3 feels like a bug. Am I missing something?

21 posts - 6 participants