Asynchronous requests HTTP.jl + LibPQ

1. HTTP.jl
I’m looking for correct solution for asynchronous handling requests by HTTP.jl. Please, do you know how should I implement this behavior?

1 thread request handling:

function requestHandler(req::HTTP.Request)::HTTP.Messages.Response
    # code
end

HTTP.serve(requestHandler, "0.0.0.0", 8080)

Is this correct?

function requestHandler(req::HTTP.Request)::HTTP.Messages.Response
    #code
end

servers = []
for i in 1:nthreads()
    push!(Threads.@spawn HTTP.serve(handler, host, 8080))
end

2. LibPQ.jl
I’m planning to use LibPQ inside these threads, do I need to configure something?

Thank you for your help!

Also, someone else needs help (HTTP.jl how to paralelise HTTP.request)

2 Likes

To answer your first question, it’s close, but here you are startup up multiple servers which are attempting to bind to the same port.

You would likely get one server working, and any other would get an error like this:

julia> servers[1]
Task (runnable) @0x00007fa7e037ecb0

julia> servers[2]
Task (failed) @0x00007fa7e037ee00
IOError: listen: address already in use (EADDRINUSE)

Only 1 server will successfully bind to a port by default.

You’ll want to pass in the parameter reuseaddr=true

for i in 1:nthreads()
    push!(Threads.@spawn HTTP.serve(handler, host, 8080, reuseaddr=true))
end

Note that the docs say that reuseaddr only works on linux.

1 Like

Thank you so much for your advice.