# How to protect a task from being blocked by other background tasks?

**URL:** <https://discourse.julialang.org/t/how-to-protect-a-task-from-being-blocked-by-other-background-tasks/83321>\
**Category:** General Usage\
**Created:** [June 24, 2022, 7:56pm UTC](https://discourse.julialang.org/t/how-to-protect-a-task-from-being-blocked-by-other-background-tasks/83321 "2022-06-24T19:56:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [June 24, 2022, 7:56pm UTC](https://discourse.julialang.org/t/how-to-protect-a-task-from-being-blocked-by-other-background-tasks/83321/1 "2022-06-24T19:56:12Z")

</div>

I have a local service that uses HTTP.jl to handle simple API requests running on the main task and then a background task (started with `Threads.@spawn`) that periodically loads data from a SQL database, does heavy computations using DataFrames and several optimization packages, and updates the data to be served by the API.

This generally works well, with HTTP response times (including some lightweight calculations ranging from 5-10 ms. But at a certain point when the heavy background calculations are being done, the response time will spike to 5-10 seconds (a 1000x increase). I am guess that this is because some of the packages like DataFrames will automatically try to run multiple tasks, which could use up all Julia threads and block the main HTTP task.

My question is how can I pin the HTTP task to a thread and prevent any other tasks from running on that thread?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 24, 2022, 8:16pm UTC](https://discourse.julialang.org/t/how-to-protect-a-task-from-being-blocked-by-other-background-tasks/83321/2 "2022-06-24T20:16:28Z")

</div>

I think you want to pin the task to a thread then. See [multithreading - Is it possible to pin a task to a particular thread? - Stack Overflow](https://stackoverflow.com/questions/65832327/is-it-possible-to-pin-a-task-to-a-particular-thread) for details.

---

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [June 24, 2022, 8:33pm UTC](https://discourse.julialang.org/t/how-to-protect-a-task-from-being-blocked-by-other-background-tasks/83321/3 "2022-06-24T20:33:32Z")

</div>

Pinning the task isn’t enough since 3rd party packages could still run tasks on all Julia threads, including the thread to which I’ve pinned my task.

---

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [June 24, 2022, 8:34pm UTC](https://discourse.julialang.org/t/how-to-protect-a-task-from-being-blocked-by-other-background-tasks/83321/4 "2022-06-24T20:34:14Z")

</div>

Looks like there is a solution to this situation that was recently merged into the 1.9 branch:  
[https://docs.julialang.org/en/v1.9-dev/manual/multi-threading/#man-threadpools](https://docs.julialang.org/en/v1.9-dev/manual/multi-threading/#man-threadpools)
