Oxygen.jl
Oxygen.jl copied to clipboard
Unexpected multithreading behavior
I'm not sure if this is a bug or expected behavior. If the latter is the case I would like to hear an explanation.
Consider the following sample code:
using Oxygen
using HTTP
example_vector = rand(1:100,500000000)
function single_thread_calc(v)
s = 0.0
for i = 1:length(v)
s += sin(v[i])
end
return s
end
@get "/calc" function(req::HTTP.Request)
@show Threads.threadid()
sum=single_thread_calc(example_vector)
return "hello, your sum is $sum"
end
serveparallel()
The execution of this endpoint will take about 7s on my PC (Windows 11, 8 Cores, 16 Threads, Julia 1.10.3, Oxygen 1.5.11, HTTP 1.10.8)
Now, if I run this code in a julia session with 4 threads assigned (i.e. julia --threads 4) and call the endpoint 4 times in quick succession (for example via swagger in 4 different browser tabs), I would expect a parallel execution of these 4 requests. What happens though is that the first request is executed and awaited and then the other 3 requests are run in parallel. This is repeatable behavior. Why does it always wait until the first request is finished before running a parallel execution? This would not be great behavior for a production scenario with a decent amount of traffic.
Thanks for the help!