# Asynchronous requests HTTP.jl + LibPQ

**URL:** <https://discourse.julialang.org/t/asynchronous-requests-http-jl-libpq/62736>\
**Category:** Performance\
**Created:** [June 11, 2021, 12:50pm UTC](https://discourse.julialang.org/t/asynchronous-requests-http-jl-libpq/62736 "2021-06-11T12:50:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michal](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Michal](https://discourse.julialang.org/u/Michal)\
**Post date:** [June 11, 2021, 12:50pm UTC](https://discourse.julialang.org/t/asynchronous-requests-http-jl-libpq/62736/1 "2021-06-11T12:50:10Z")

</div>

**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:

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

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

```

Is this correct?

```julia
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](https://discourse.julialang.org/t/http-jl-how-to-paralelise-http-request/43569))

---

<div class="post-metadata">

**Author:** ![caleb-allen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caleb-allen/32/14054_2.png) [@caleb-allen](https://discourse.julialang.org/u/caleb-allen)\
**Post date:** [June 11, 2021, 8:59pm UTC](https://discourse.julialang.org/t/asynchronous-requests-http-jl-libpq/62736/2 "2021-06-11T20:59:13Z")

</div>

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
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`

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

```

Note that the [docs](https://juliaweb.github.io/HTTP.jl/stable/public_interface/#HTTP.Handlers.serve) say that `reuseaddr` only works on linux.

---

<div class="post-metadata">

**Author:** ![Michal](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Michal](https://discourse.julialang.org/u/Michal)\
**Post date:** [June 14, 2021, 5:10am UTC](https://discourse.julialang.org/t/asynchronous-requests-http-jl-libpq/62736/3 "2021-06-14T05:10:30Z")

</div>

Thank you so much for your advice.
