# Dedicated threads for handling requests to endpoints with HTTP.jl

**URL:** <https://discourse.julialang.org/t/dedicated-threads-for-handling-requests-to-endpoints-with-http-jl/76339>\
**Category:** General Usage\
**Tags:** question, web, multithreading, http\
**Created:** [February 13, 2022, 10:28am UTC](https://discourse.julialang.org/t/dedicated-threads-for-handling-requests-to-endpoints-with-http-jl/76339 "2022-02-13T10:28:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thazhemadam](https://avatars.discourse-cdn.com/v4/letter/t/ad7895/32.png) [@thazhemadam](https://discourse.julialang.org/u/thazhemadam)\
**Post date:** [February 13, 2022, 10:28am UTC](https://discourse.julialang.org/t/dedicated-threads-for-handling-requests-to-endpoints-with-http-jl/76339/1 "2022-02-13T10:28:29Z")

</div>

Say I’m running a `HTTP.jl` server, that serves two endpoints `/one` and `/two`.

Requests to `/one` will start a computation-based task asynchronously and return a `Response` with a `200` status code. This task can be assumed to take some time to execute.

On the other hand, `/two` is as an endpoint that doesn’t start a compute-intensive task but is used for logging purposes (i.e., the client uses the response to perform updates/logging that is based on the server’s internal state that is returned). This response can essentially be instantaneously returned.

```julia
using HTTP
const ROUTER = HTTP.Router()
HTTP.@register(ROUTER, "POST", "/one", start_compute_intensive_task)
HTTP.@register(ROUTER, "GET", "/two", get_logging_values)

```

Now, suppose a client makes a `POST` request to `/one`, followed by a `GET` request to `/two`.  
The task started by `start_compute_intensive_task` essentially takes up all the compute provided by the server and thus **significantly** delays the request(s) made to `/two` from being handled.

Is there any way, to assign say one thread dedicated to handling requests made to `/two`?

Is there any other way I can ensure that requests made to `/two` are always instantly handled, even if there’s a computationally intensive task started by `start_compute_intensive_task` running already?

---

<div class="post-metadata">

**Author:** ![yoh-meyers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yoh-meyers/32/14700_2.png) [@yoh-meyers](https://discourse.julialang.org/u/yoh-meyers)\
**Post date:** [February 16, 2022, 9:14am UTC](https://discourse.julialang.org/t/dedicated-threads-for-handling-requests-to-endpoints-with-http-jl/76339/2 "2022-02-16T09:14:58Z")

</div>

You may want to consider `Threads.foreach` ([Multi-Threading · The Julia Language](https://docs.julialang.org/en/v1/base/multi-threading/#Base.Threads.foreach)), specifying limit to `n-1` of available threads. That is for `start_compute_intensive_task` function.

So always have 1 spare thread available for `get_logging_values`

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [February 16, 2022, 10:24am UTC](https://discourse.julialang.org/t/dedicated-threads-for-handling-requests-to-endpoints-with-http-jl/76339/3 "2022-02-16T10:24:37Z")

</div>

You can probably adapt the example I posted here: [HTTP.jl#798](https://github.com/JuliaWeb/HTTP.jl/issues/798#issuecomment-1019969735).

---

<div class="post-metadata">

**Author:** ![thazhemadam](https://avatars.discourse-cdn.com/v4/letter/t/ad7895/32.png) [@thazhemadam](https://discourse.julialang.org/u/thazhemadam)\
**Post date:** [February 22, 2022, 7:47pm UTC](https://discourse.julialang.org/t/dedicated-threads-for-handling-requests-to-endpoints-with-http-jl/76339/4 "2022-02-22T19:47:23Z")

</div>

Thanks, @fredrikekre! A slightly different variant of your example that was more compatible with my use case is what I ended up doing. 🙂
