I have a scenario in my Python application where I have some work that needs to be done in the background, but in a fire-and-forget manner. There are two constraints I'm trying to satisfy:
- The background task uses PyJulia, which is not thread-safe and will fail if it is invoked from two different threads in the same process, even if these calls don't happen concurrently.
- It turns out PyJulia also has some heavy lifting to do the first time it is invoked in a given process. It takes a few seconds to initialize before the Julia code gets executed. I don't want to pay this cost every time I call it.
What's the simplest way of handling a situation like this? I imagine that I have to create a single long-lived worker thread or process to which I can continually queue tasks, but I don't want to write custom code to handle all of this if I don't have to.