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

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?

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 for details.

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.

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

1 Like