Background thread for periodic updates

#1

Hey guys, I am facing the following issue. I have two main processes:

  1. one which updates the state of some of the objects in a fixed interval (e.g. every 100 milliseconds),
  2. the other one which uses this state to perform some action.

My current implementation uses the 1) as an @async process, while 2) is the main process. This is fine in most cases, however in the case that some computation in 2) is taking too long (e.g. some matrix multiplication), I might miss a full update cycle in 1), which is harmful for my application.

My original code was in Python, and there it was resolved using threading.Thread(), which would do these updates in a completely separate thread. Reading up on documentation, I cannot find a simple way of achieving this in Julia. Is this currently possible? If yes, how? If not, is it planned?

#2

Before too long, hopefully, tasks will have the option to run in separate threads:

Until then, you could use Base.@threadcall, assuming you are careful about thread safety.

2 Likes
#3

I am facing this problem in UI programming as well. One can use Julias multi-process facilities which, however, makes the code much more complex. I am currently using a solution similar to yours but the heavy calculation is split into many smaller ones and the code is yielding in between. This works quite well.

#4

Thanks for the link, I will follow the issue!

Until then, you could use Base.@threadcall , assuming you are careful about thread safety.

The documentation is very short on Base.@threadcall, without a single example. I am afraid that I would need much more experience with multi-threaded programming to be able to be sufficiently threadsafe. :confused:

One can use Julias multi-process facilities which, however, makes the code much more complex.

Correct me if I am wrong, but my understanding was that the overhead caused by using a multi-process approach for updating a shared state makes this a nearly unusable solution. From what I’ve read, multi-threading should be the perfect middle-ground for this.

I am currently using a solution similar to yours but the heavy calculation is split into many smaller ones and the code is yielding in between.

Makes sense, will definitely aim for a similar approach in the meantime. Thanks!