No real multithreading possible?

Hey there,

I am desperately trying to figure out how to do some “real” multi-threadding in Julia. What do I mean by that: My program consists of three tasks: the main task and two separate task I start. I need to communicate from the main task to the two separte tasks via channels. This is how I start my desired background task:

c = Channel{Bool}(1, spawn=true) do c # create new channel with buffersize 1
    status = false
    while isopen(c)
        if isready(c) # determine whether the channel `c` has a value stored to it; returns immediately, does not block
            status = fetch(c) # wait for and get the available item from the channel `c`
        end
        @info "$(Dates.now()) status is $status"

        t_now = Dates.now()
        while Dates.now() < t_now + Dates.Second(10)
            sleep(1) # [s]
        end
    end
    @info "channel closed, background task will terminate"
end

I update the channel value by

function updateChannelValue!(c::Channel, value::Bool)
    # Since the channel capacity is only one and `put!` blocks if there is
    # already a value present, we must first make sure to remove the element stored there.
    lock(c)
    try
        if isready(c) # determine whether the channel `c` has a value stored to it; returns immediately, does not block
            take!(c) # remove old value from channel
        end
        put!(c,value) 
    finally
        unlock(c)
    end
    return        
end

updateChannelValue!(c, true)

So far everything works like expected. But If i do

for i = 1:10_000
    x = rand(100_000)
    y = x.^2 .+ log.(x) + x.^10 + log10.(x)
end

I can see that until the loop is finished there is no output from the background task. However, if I do

for i = 1:10_000
    x = rand(100_000)
    y = x.^2 .+ log.(x) + x.^10 + log10.(x)
    yield() # switch to the scheduler to allow another scheduled task to run
end

It works as I would like it. The problem is that this is just an example. I my real code I could have (pure Julia and non-pure Julia) calls that takes longer than e.g. 60 seconds and I want my background task to run at least (more or less reliable) every 60 seconds.

Here is the whole test script:

@info "Julia has $(Threads.nthreads()) cores available"
@info "REPL runs on core $(Threads.threadid())"
c = Channel{Bool}(1, spawn=true) do c # create new channel with buffersize 1
    @info "Background task runs on core $(Threads.threadid())"
    status = false
    while isopen(c)
        if isready(c) # determine whether the channel `c` has a value stored to it; returns immediately, does not block
            status = fetch(c) # wait for and get the available item from the channel `c`
        end
        @info "$(Dates.now()) status is $status"

        t_now = Dates.now()
        while Dates.now() < t_now + Dates.Second(10)
            sleep(1) # [s]
        end
    end
    @info "channel closed, background task will terminate"
end

function updateChannelValue!(c::Channel, value::Bool)
    # Since the channel capacity is only one and `put!` blocks if there is
    # already a value present, we must first make sure to remove the element stored there.
    lock(c)
    try
        if isready(c) # determine whether the channel `c` has a value stored to it; returns immediately, does not block
            take!(c) # remove old value from channel
        end
        put!(c,value) 
    finally
        unlock(c)
    end
    return        
end

sleep(30)
updateChannelValue!(c, true)

sleep(60)
@info "call blocking code"
# this will block the background task until finished
for i = 1:10_000
    x = rand(100_000)
    y = x.^2 .+ log.(x) + x.^10 + log10.(x)
end

@info "call non-blocking code"
# this won't block the background task 
for i = 1:10_000
    x = rand(100_000)
    y = x.^2 .+ log.(x) + x.^10 + log10.(x)
    yield() # switch to the scheduler to allow another scheduled task to run
end

close(c)

and the output on my machine:

[ Info: Julia has 2 cores available
[ Info: REPL runs on core 1
[ Info: Background task runs on core 2
[ Info: 2022-03-15T17:02:10.591 status is false
[ Info: 2022-03-15T17:02:20.624 status is false
[ Info: 2022-03-15T17:02:30.664 status is false
[ Info: 2022-03-15T17:02:40.696 status is true
[ Info: 2022-03-15T17:02:50.743 status is true
[ Info: 2022-03-15T17:03:00.781 status is true
[ Info: 2022-03-15T17:03:10.822 status is true
[ Info: 2022-03-15T17:03:20.854 status is true
[ Info: 2022-03-15T17:03:30.902 status is true
[ Info: call blocking code
[ Info: call non-blocking code
[ Info: 2022-03-15T17:05:15.381 status is true
[ Info: 2022-03-15T17:05:25.537 status is true
[ Info: 2022-03-15T17:05:35.591 status is true
[ Info: 2022-03-15T17:05:45.672 status is true
[ Info: 2022-03-15T17:05:55.729 status is true
[ Info: 2022-03-15T17:06:05.808 status is true
[ Info: 2022-03-15T17:06:15.926 status is true
[ Info: 2022-03-15T17:06:25.970 status is true
[ Info: 2022-03-15T17:06:36.055 status is true

Hope there is any hint … what I could do

If I have > 2 threads (I tried with 8) and if I prepend the “blocking” for-loop with Threads.@spawn then the loop no longer blocks. I’m not sure why running code on the main thread is blocking (though it’s possible it only blocks printing to the REPL, or something like that).

I also tried running the whole thing in a separate task, but the for loop(not in the main thread anymore) still blocks the main thread.

UPDATE: I scheduled the task in the main thread… should be fixed now. This also fixed the problem with the blocking.

Example code
import Dates
@info "Julia has $(Threads.nthreads()) cores available"
@info "REPL runs on core $(Threads.threadid())"


function updateChannelValue!(c::Channel, value::Bool)
    # Since the channel capacity is only one and `put!` blocks if there is
    # already a value present, we must first make sure to remove the element stored there.
    lock(c)
    try
        if isready(c) # determine whether the channel `c` has a value stored to it; returns immediately, does not block
            take!(c) # remove old value from channel
        end
        put!(c,value) 
    finally
        unlock(c)
    end
    return        
end

function main()
	@info "$(Dates.now()) main runs on core $(Threads.threadid())"
	c = Channel{Bool}(1, spawn=true) do c # create new channel with buffersize 1
		@info "$(Dates.now()) Background task runs on core $(Threads.threadid())"
		status = false
		while isopen(c)
			if isready(c) # determine whether the channel `c` has a value stored to it; returns immediately, does not block
				status = fetch(c) # wait for and get the available item from the channel `c`
			end
			@info "$(Dates.now()) status is $status $(Threads.threadid())"

			t_now = Dates.now()
			while Dates.now() < t_now + Dates.Second(1)
				sleep(1) # [s]
			end
		end
		@info "$(Dates.now()) channel closed, background task will terminate $(Threads.threadid())"
	end

	@info "$(Dates.now()) sleep 2 $(Threads.threadid())"
	sleep(2)
	@info "$(Dates.now()) update channel $(Threads.threadid())"
	updateChannelValue!(c, true)
	@info "$(Dates.now()) sleep 2 $(Threads.threadid())"
	sleep(2)
	@info "$(Dates.now()) call blocking code $(Threads.threadid())"
	# this will block the background task until finished
	x = rand(100_000)
	for i = 1:10_00
		y = x.^2 .+ log.(x) + x.^10 + log10.(x)
	end
	@info "$(Dates.now()) blocking code done $(Threads.threadid())"

	@info "$(Dates.now()) call non-blocking code $(Threads.threadid())"
	# this won't block the background task 
	for i = 1:10_00
		x = rand(100_000)
		y = x.^2 .+ log.(x) + x.^10 + log10.(x)
		yield() # switch to the scheduler to allow another scheduled task to run
	end
	@info "$(Dates.now()) non-blocking code done $(Threads.threadid())"
	close(c)
end

task = Threads.@spawn main()
while !istaskdone(task)
	sleep(1)
	@info "$(Dates.now()) ping $(Threads.threadid())"
end



Output
 julia -t auto .\demo.jl
[ Info: Julia has 8 cores available
[ Info: REPL runs on core 1
[ Info: 2022-03-15T19:41:44.135 main runs on core 3
[ Info: 2022-03-15T19:41:44.216 sleep 2 3
[ Info: 2022-03-15T19:41:44.306 Background task runs on core 2
[ Info: 2022-03-15T19:41:44.306 status is false 2
[ Info: 2022-03-15T19:41:45.189 ping 1
[ Info: 2022-03-15T19:41:45.309 status is false 2
[ Info: 2022-03-15T19:41:46.192 ping 1
[ Info: 2022-03-15T19:41:46.244 update channel 3
[ Info: 2022-03-15T19:41:46.244 sleep 2 3
[ Info: 2022-03-15T19:41:46.324 status is true 2
[ Info: 2022-03-15T19:41:47.207 ping 1
[ Info: 2022-03-15T19:41:47.344 status is true 2
[ Info: 2022-03-15T19:41:48.209 ping 1
[ Info: 2022-03-15T19:41:48.246 call blocking code 3
[ Info: 2022-03-15T19:41:48.346 status is true 2
[ Info: 2022-03-15T19:41:49.217 ping 1
[ Info: 2022-03-15T19:41:49.360 status is true 2
[ Info: 2022-03-15T19:41:50.223 ping 1
[ Info: 2022-03-15T19:41:50.367 status is true 2
[ Info: 2022-03-15T19:41:51.224 ping 1
[ Info: 2022-03-15T19:41:51.387 status is true 2
[ Info: 2022-03-15T19:41:52.230 ping 1
[ Info: 2022-03-15T19:41:52.393 status is true 2
[ Info: 2022-03-15T19:41:53.245 ping 1
[ Info: 2022-03-15T19:41:53.398 status is true 2
[ Info: 2022-03-15T19:41:54.251 ping 1
[ Info: 2022-03-15T19:41:54.414 status is true 2
[ Info: 2022-03-15T19:41:54.942 blocking code done 3
[ Info: 2022-03-15T19:41:54.944 call non-blocking code 3
[ Info: 2022-03-15T19:41:55.269 ping 1
[ Info: 2022-03-15T19:41:55.416 status is true 2
[ Info: 2022-03-15T19:41:56.284 ping 1
[ Info: 2022-03-15T19:41:56.425 status is true 2
[ Info: 2022-03-15T19:41:57.294 ping 1
[ Info: 2022-03-15T19:41:57.434 status is true 2
[ Info: 2022-03-15T19:41:58.293 ping 1
[ Info: 2022-03-15T19:41:58.453 status is true 2
[ Info: 2022-03-15T19:41:59.304 ping 1
[ Info: 2022-03-15T19:41:59.466 status is true 2
[ Info: 2022-03-15T19:42:00.310 ping 1
[ Info: 2022-03-15T19:42:00.470 status is true 2
[ Info: 2022-03-15T19:42:01.322 ping 1
[ Info: 2022-03-15T19:42:01.486 status is true 2
[ Info: 2022-03-15T19:42:02.336 ping 1
[ Info: 2022-03-15T19:42:02.498 status is true 2
[ Info: 2022-03-15T19:42:02.764 non-blocking code done 3
[ Info: 2022-03-15T19:42:03.342 ping 1

Also, it seems the syntax highlighting does not work for interpolated strings.

Thanks @feanor12 for your solution! BUT it seems to work only if you have enough logical cores available! On my system I get:

[ Info: Julia has 2 cores available
[ Info: REPL runs on core 1
[ Info: 2022-03-16T08:33:26.947 main runs on core 2
[ Info: 2022-03-16T08:33:27.089 sleep 2 2
[ Info: 2022-03-16T08:33:27.234 Background task runs on core 2
[ Info: 2022-03-16T08:33:27.234 status is false 2
[ Info: 2022-03-16T08:33:28.088 ping 1
[ Info: 2022-03-16T08:33:28.237 status is false 2
[ Info: 2022-03-16T08:33:29.094 ping 1
[ Info: 2022-03-16T08:33:29.240 update channel 2
[ Info: 2022-03-16T08:33:29.240 status is false 2
[ Info: 2022-03-16T08:33:29.240 sleep 2 2
[ Info: 2022-03-16T08:33:30.098 ping 1
[ Info: 2022-03-16T08:33:30.247 status is true 2
[ Info: 2022-03-16T08:33:31.101 ping 1
[ Info: 2022-03-16T08:33:31.246 call blocking code 2
[ Info: 2022-03-16T08:33:32.676 ping 1
[ Info: 2022-03-16T08:33:33.681 ping 1
[ Info: 2022-03-16T08:33:34.685 ping 1
[ Info: 2022-03-16T08:33:35.688 ping 1
[ Info: 2022-03-16T08:33:36.691 ping 1
[ Info: 2022-03-16T08:33:37.719 ping 1
[ Info: 2022-03-16T08:33:38.721 ping 1
[ Info: 2022-03-16T08:33:39.726 ping 1
[ Info: 2022-03-16T08:33:40.271 blocking code done 2
[ Info: 2022-03-16T08:33:40.271 status is true 2
[ Info: 2022-03-16T08:33:40.272 call non-blocking code 2
[ Info: 2022-03-16T08:33:40.731 ping 1
[ Info: 2022-03-16T08:33:41.284 status is true 2
[ Info: 2022-03-16T08:33:41.735 ping 1
[ Info: 2022-03-16T08:33:42.319 status is true 2
[ Info: 2022-03-16T08:33:42.737 ping 1
[ Info: 2022-03-16T08:33:43.337 status is true 2
[ Info: 2022-03-16T08:33:43.746 ping 1
[ Info: 2022-03-16T08:33:44.352 status is true 2
[ Info: 2022-03-16T08:33:44.752 ping 1
[ Info: 2022-03-16T08:33:45.361 status is true 2
[ Info: 2022-03-16T08:33:45.754 ping 1
[ Info: 2022-03-16T08:33:46.379 status is true 2
[ Info: 2022-03-16T08:33:46.757 ping 1
[ Info: 2022-03-16T08:33:47.397 status is true 2
[ Info: 2022-03-16T08:33:47.761 ping 1
[ Info: 2022-03-16T08:33:48.416 status is true 2
[ Info: 2022-03-16T08:33:48.767 ping 1
[ Info: 2022-03-16T08:33:48.910 non-blocking code done 2
[ Info: 2022-03-16T08:33:49.435 channel closed, background task will terminate 2
[ Info: 2022-03-16T08:33:49.772 ping 1

so you can see that the “blocking code” running on core #2 is blocking the background task, which is also running on core #2, but is not blocking the main loop running on core #1. It seems that “real” multi-threading is possible with the meantioned “trick” of using a main function an calling it using Threads.@spawn but it only works if there are sufficiently number of cores. If there is no other solution I think there must be something done in the internal Julia scheduler to enable quasi-parallel multi-threading even on a single-core (like it’s done by every OS).

However, in my case I am “lucky”, because the target device for the code is a raspberry pi 4 with a quad core CPU and, including the main loop, I would need exactly 4 cores to run my software in the current design … here the test output on the raspi, as in the output provided by @feanor12, here it works like I want:

[ Info: Julia has 4 cores available
[ Info: REPL runs on core 1
[ Info: 2022-03-16T07:42:17.862 main runs on core 3
[ Info: 2022-03-16T07:42:18.191 sleep 2 3
[ Info: 2022-03-16T07:42:18.454 Background task runs on core 2
[ Info: 2022-03-16T07:42:18.455 status is false 2
[ Info: 2022-03-16T07:42:19.038 ping 1
[ Info: 2022-03-16T07:42:19.457 status is false 2
[ Info: 2022-03-16T07:42:20.040 ping 1
[ Info: 2022-03-16T07:42:20.192 update channel 3
[ Info: 2022-03-16T07:42:20.196 sleep 2 3
[ Info: 2022-03-16T07:42:20.458 status is true 2
[ Info: 2022-03-16T07:42:21.042 ping 1
[ Info: 2022-03-16T07:42:21.460 status is true 2
[ Info: 2022-03-16T07:42:22.044 ping 1
[ Info: 2022-03-16T07:42:22.197 call blocking code 3
[ Info: 2022-03-16T07:42:22.462 status is true 2
[ Info: 2022-03-16T07:42:23.046 ping 1
[ Info: 2022-03-16T07:42:23.464 status is true 2
[ Info: 2022-03-16T07:42:24.047 ping 1
[ Info: 2022-03-16T07:42:24.465 status is true 2
[ Info: 2022-03-16T07:42:25.050 ping 1
[ Info: 2022-03-16T07:42:25.467 status is true 2
[ Info: 2022-03-16T07:42:26.052 ping 1
[ Info: 2022-03-16T07:42:26.468 status is true 2
[ Info: 2022-03-16T07:42:27.055 ping 1
[ Info: 2022-03-16T07:42:27.470 status is true 2
[ Info: 2022-03-16T07:42:28.056 ping 1
[ Info: 2022-03-16T07:42:28.473 status is true 2
[ Info: 2022-03-16T07:42:29.059 ping 1
[ Info: 2022-03-16T07:42:29.474 status is true 2
[ Info: 2022-03-16T07:42:30.060 ping 1
[ Info: 2022-03-16T07:42:30.476 status is true 2
[ Info: 2022-03-16T07:42:31.062 ping 1
[ Info: 2022-03-16T07:42:31.479 status is true 2
[ Info: 2022-03-16T07:42:32.064 ping 1
[ Info: 2022-03-16T07:42:32.481 status is true 2
[ Info: 2022-03-16T07:42:33.066 ping 1
[ Info: 2022-03-16T07:42:33.483 status is true 2
[ Info: 2022-03-16T07:42:34.067 ping 1
[ Info: 2022-03-16T07:42:34.485 status is true 2
[ Info: 2022-03-16T07:42:35.069 ping 1
[ Info: 2022-03-16T07:42:35.486 status is true 2
[ Info: 2022-03-16T07:42:36.072 ping 1
[ Info: 2022-03-16T07:42:36.489 status is true 2
[ Info: 2022-03-16T07:42:37.073 ping 1
[ Info: 2022-03-16T07:42:37.490 status is true 2
[ Info: 2022-03-16T07:42:38.076 ping 1
[ Info: 2022-03-16T07:42:38.492 status is true 2
[ Info: 2022-03-16T07:42:39.078 ping 1
[ Info: 2022-03-16T07:42:39.495 status is true 2
[ Info: 2022-03-16T07:42:40.079 ping 1
[ Info: 2022-03-16T07:42:40.497 status is true 2
[ Info: 2022-03-16T07:42:41.082 ping 1
[ Info: 2022-03-16T07:42:41.498 status is true 2
[ Info: 2022-03-16T07:42:42.083 ping 1
[ Info: 2022-03-16T07:42:42.500 status is true 2
[ Info: 2022-03-16T07:42:43.086 ping 1
[ Info: 2022-03-16T07:42:43.502 status is true 2
[ Info: 2022-03-16T07:42:44.087 ping 1
[ Info: 2022-03-16T07:42:44.505 status is true 2
[ Info: 2022-03-16T07:42:45.088 ping 1
[ Info: 2022-03-16T07:42:45.506 status is true 2
[ Info: 2022-03-16T07:42:46.090 ping 1
[ Info: 2022-03-16T07:42:46.508 status is true 2
[ Info: 2022-03-16T07:42:47.092 ping 1
[ Info: 2022-03-16T07:42:47.511 status is true 2
[ Info: 2022-03-16T07:42:48.095 ping 1
[ Info: 2022-03-16T07:42:48.513 status is true 2
[ Info: 2022-03-16T07:42:49.096 ping 1
[ Info: 2022-03-16T07:42:49.515 status is true 2
[ Info: 2022-03-16T07:42:50.098 ping 1
[ Info: 2022-03-16T07:42:50.517 status is true 2
[ Info: 2022-03-16T07:42:51.100 ping 1
[ Info: 2022-03-16T07:42:51.518 status is true 2
[ Info: 2022-03-16T07:42:52.105 ping 1
[ Info: 2022-03-16T07:42:52.521 status is true 2
[ Info: 2022-03-16T07:42:53.107 ping 1
[ Info: 2022-03-16T07:42:53.522 status is true 2
[ Info: 2022-03-16T07:42:54.109 ping 1
[ Info: 2022-03-16T07:42:54.524 status is true 2
[ Info: 2022-03-16T07:42:55.112 ping 1
[ Info: 2022-03-16T07:42:55.526 status is true 2
[ Info: 2022-03-16T07:42:56.114 ping 1
[ Info: 2022-03-16T07:42:56.529 status is true 2
[ Info: 2022-03-16T07:42:57.116 ping 1
[ Info: 2022-03-16T07:42:57.532 status is true 2
[ Info: 2022-03-16T07:42:58.117 ping 1
[ Info: 2022-03-16T07:42:58.533 status is true 2
[ Info: 2022-03-16T07:42:59.120 ping 1
[ Info: 2022-03-16T07:42:59.535 status is true 2
[ Info: 2022-03-16T07:43:00.121 ping 1
[ Info: 2022-03-16T07:43:00.537 status is true 2
[ Info: 2022-03-16T07:43:00.918 blocking code done 3
[ Info: 2022-03-16T07:43:00.919 call non-blocking code 3
[ Info: 2022-03-16T07:43:01.123 ping 1
[ Info: 2022-03-16T07:43:01.539 status is true 2
[ Info: 2022-03-16T07:43:02.125 ping 1
[ Info: 2022-03-16T07:43:02.542 status is true 2
[ Info: 2022-03-16T07:43:03.126 ping 1
[ Info: 2022-03-16T07:43:03.544 status is true 2
[ Info: 2022-03-16T07:43:04.127 ping 1
[ Info: 2022-03-16T07:43:04.546 status is true 2
[ Info: 2022-03-16T07:43:05.129 ping 1
[ Info: 2022-03-16T07:43:05.547 status is true 2
[ Info: 2022-03-16T07:43:06.132 ping 1
[ Info: 2022-03-16T07:43:06.550 status is true 2
[ Info: 2022-03-16T07:43:07.134 ping 1
[ Info: 2022-03-16T07:43:07.553 status is true 2
[ Info: 2022-03-16T07:43:08.135 ping 1
[ Info: 2022-03-16T07:43:08.555 status is true 2
[ Info: 2022-03-16T07:43:09.136 ping 1
[ Info: 2022-03-16T07:43:09.557 status is true 2
[ Info: 2022-03-16T07:43:10.138 ping 1
[ Info: 2022-03-16T07:43:10.559 status is true 2
[ Info: 2022-03-16T07:43:11.139 ping 1
[ Info: 2022-03-16T07:43:11.562 status is true 2
[ Info: 2022-03-16T07:43:12.140 ping 1
[ Info: 2022-03-16T07:43:12.564 status is true 2
[ Info: 2022-03-16T07:43:13.142 ping 1
[ Info: 2022-03-16T07:43:13.566 status is true 2
[ Info: 2022-03-16T07:43:14.144 ping 1
[ Info: 2022-03-16T07:43:14.568 status is true 2
[ Info: 2022-03-16T07:43:15.146 ping 1
[ Info: 2022-03-16T07:43:15.571 status is true 2
[ Info: 2022-03-16T07:43:16.147 ping 1
[ Info: 2022-03-16T07:43:16.574 status is true 2
[ Info: 2022-03-16T07:43:17.148 ping 1
[ Info: 2022-03-16T07:43:17.575 status is true 2
[ Info: 2022-03-16T07:43:18.150 ping 1
[ Info: 2022-03-16T07:43:18.578 status is true 2
[ Info: 2022-03-16T07:43:19.152 ping 1
[ Info: 2022-03-16T07:43:19.579 status is true 2
[ Info: 2022-03-16T07:43:20.154 ping 1
[ Info: 2022-03-16T07:43:20.580 status is true 2
[ Info: 2022-03-16T07:43:21.156 ping 1
[ Info: 2022-03-16T07:43:21.583 status is true 2
[ Info: 2022-03-16T07:43:22.159 ping 1
[ Info: 2022-03-16T07:43:22.585 status is true 2
[ Info: 2022-03-16T07:43:23.160 ping 1
[ Info: 2022-03-16T07:43:23.588 status is true 2
[ Info: 2022-03-16T07:43:24.162 ping 1
[ Info: 2022-03-16T07:43:24.590 status is true 2
[ Info: 2022-03-16T07:43:25.163 ping 1
[ Info: 2022-03-16T07:43:25.592 status is true 2
[ Info: 2022-03-16T07:43:26.166 ping 1
[ Info: 2022-03-16T07:43:26.595 status is true 2
[ Info: 2022-03-16T07:43:27.167 ping 1
[ Info: 2022-03-16T07:43:27.596 status is true 2
[ Info: 2022-03-16T07:43:28.169 ping 1
[ Info: 2022-03-16T07:43:28.598 status is true 2
[ Info: 2022-03-16T07:43:29.171 ping 1
[ Info: 2022-03-16T07:43:29.601 status is true 2
[ Info: 2022-03-16T07:43:30.173 ping 1
[ Info: 2022-03-16T07:43:30.603 status is true 2
[ Info: 2022-03-16T07:43:31.174 ping 1
[ Info: 2022-03-16T07:43:31.605 status is true 2
[ Info: 2022-03-16T07:43:32.176 ping 1
[ Info: 2022-03-16T07:43:32.607 status is true 2
[ Info: 2022-03-16T07:43:33.178 ping 1
[ Info: 2022-03-16T07:43:33.608 status is true 2
[ Info: 2022-03-16T07:43:34.180 ping 1
[ Info: 2022-03-16T07:43:34.610 status is true 2
[ Info: 2022-03-16T07:43:35.181 ping 1
[ Info: 2022-03-16T07:43:35.613 status is true 2
[ Info: 2022-03-16T07:43:36.183 ping 1
[ Info: 2022-03-16T07:43:36.614 status is true 2
[ Info: 2022-03-16T07:43:37.185 ping 1
[ Info: 2022-03-16T07:43:37.617 status is true 2
[ Info: 2022-03-16T07:43:38.188 ping 1
[ Info: 2022-03-16T07:43:38.619 status is true 2
[ Info: 2022-03-16T07:43:39.190 ping 1
[ Info: 2022-03-16T07:43:39.295 non-blocking code done 3
[ Info: 2022-03-16T07:43:39.620 channel closed, background task will terminate 2
[ Info: 2022-03-16T07:43:40.191 ping 1

Some of this is discussed in Severe thread starvation issues · Issue #41586 · JuliaLang/julia · GitHub, specifically task preemption and implicit yield points

@paulmelis: Thanks for the hint! Good to see that this is an known isse and work-in-progress. Hope the solution makes its way soon into a stable Julia release. In the meantine I will stick to the suggested solution as I works for me as a workaround.

1 Like

Most language has no priority. I suggest Julialang provide true Timer with only high priority to give an interrupt entry for callback functions. Thus, the programmer can get more efficient software.
For example:

  1. timeout function
  2. pure computing task can be terminated in time
  3. problem here could be solved just by create a Timer with one sentence schedule function as callback

I think this may be helpful, I thought in 1.8, but only 1.9 NEWS, so can you download the nightly build and check with it?

The --threads command-line option now accepts auto|N[,auto|M] where M specifies the number of interactive threads to create (auto currently means 1)

I didn’t read/analyse fully what your problem was, to know if this applies, but at least it should be a simple test.

I test your solution one minute ago, and the output is exactly as the code of @agcm : there is no outputs between “call blocking code” and “call non-blocking code”.

With julia -q -t 4 starting julia verion 1.8.0.

@Palli