Ok, I tried to get a basic MWE of a server which handles low-latency tasks on the interactive thread (where the main server loop is run) and spawns a default thread for the high-latency task.
Here is the server script:
using Dates, HTTP
const ROUTER = HTTP.Router()
function low_latency(req)
Threads.@spawn :interactive begin # also tried without this
@info "$(now())> $(Threads.threadpool()) tid: $(Threads.threadid())"
return "Done with low latency task"
end
end
HTTP.register!(ROUTER, "GET", "/low", low_latency)
function high_latency(req)
Threads.@spawn begin
s = sum(randn(10^8))
@info "$(now())> $(Threads.threadpool()) tid: $(Threads.threadid()), sum = $s"
return "Done with high latency task"
end
end
HTTP.register!(ROUTER, "GET", "/high", high_latency)
function requestHandler(req)
resp = ROUTER(req)
return HTTP.Response(200, resp)
end
HTTP.serve(requestHandler, "0.0.0.0", 7777)
So I have a couple of questions:
- When I run the server with
Julia -t N,1
where N>1
, I get an error when hitting it with a request: Error: handle_connection handler error, exception = β AssertionError: 0 < tid <= v
Full stacktrace
β Error: handle_connection handler error
β exception =
β AssertionError: 0 < tid <= v
β Stacktrace:
β [1] _length_assert()
β @ URIs ~/.julia/packages/URIs/6ecRe/src/URIs.jl:694
β [2] access_threaded(f::typeof(URIs.uri_reference_regex_f), v::Vector{URIs.RegexAndMatchData})
β @ URIs ~/.julia/packages/URIs/6ecRe/src/URIs.jl:685
β [3] parse_uri_reference(str::String; strict::Bool)
β @ URIs ~/.julia/packages/URIs/6ecRe/src/URIs.jl:125
β [4] parse_uri_reference
β @ ~/.julia/packages/URIs/6ecRe/src/URIs.jl:123 [inlined]
β [5] URI
β @ ~/.julia/packages/URIs/6ecRe/src/URIs.jl:146 [inlined]
β [6] gethandler
β @ ~/.julia/packages/HTTP/z8l0i/src/Handlers.jl:391 [inlined]
β [7] (::HTTP.Handlers.Router{typeof(HTTP.Handlers.default404), typeof(HTTP.Handlers.default405), Nothing})(req::HTTP.Messages.Request)
β @ HTTP.Handlers ~/.julia/packages/HTTP/z8l0i/src/Handlers.jl:427
β [8] requestHandler(req::HTTP.Messages.Request)
β @ Main ./REPL[7]:2
β [9] (::HTTP.Handlers.var"#1#2"{typeof(requestHandler)})(stream::HTTP.Streams.Stream{HTTP.Messages.Request, HTTP.ConnectionPool.Connection{Sockets.TCPSocket}})
β @ HTTP.Handlers ~/.julia/packages/HTTP/z8l0i/src/Handlers.jl:58
β [10] #invokelatest#2
β @ ./essentials.jl:816 [inlined]
β [11] invokelatest
β @ ./essentials.jl:813 [inlined]
β [12] handle_connection(f::Function, c::HTTP.ConnectionPool.Connection{Sockets.TCPSocket}, listener::HTTP.Servers.Listener{Nothing, Sockets.TCPServer}, readtimeout::Int64, access_log::Nothing)
β @ HTTP.Servers ~/.julia/packages/HTTP/z8l0i/src/Servers.jl:447
β [13] macro expansion
β @ ~/.julia/packages/HTTP/z8l0i/src/Servers.jl:385 [inlined]
β [14] (::HTTP.Servers.var"#16#17"{HTTP.Handlers.var"#1#2"{typeof(requestHandler)}, HTTP.Servers.Listener{Nothing, Sockets.TCPServer}, Set{HTTP.ConnectionPool.Connection}, Int64, Nothing, Base.Semaphore, HTTP.ConnectionPool.Connection{Sockets.TCPSocket}})()
β @ HTTP.Servers ./task.jl:514
β @ HTTP.Servers ~/.julia/packages/HTTP/z8l0i/src/Servers.jl:461
- When I run the server with
Julia -t 1,1
I get no error but note that
julia> fetch(Threads.@spawn :interactive Threads.threadpool())
:default
so both tasks end up running on the same thread anyways, which affects latency.
So Iβm obviously doing something wrong. Could someone help me get back on track?