# Background thread for periodic updates

**URL:** https://discourse.julialang.org/t/background-thread-for-periodic-updates/20786
**Category:** General Usage
**Created:** [February 14, 2019, 1:16pm UTC](https://discourse.julialang.org/t/background-thread-for-periodic-updates/20786 "2019-02-14T13:16:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![cedeerwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cedeerwe/32/8552_2.png) [@cedeerwe](https://discourse.julialang.org/u/cedeerwe)
#### Post date: [February 14, 2019, 1:16pm UTC](https://discourse.julialang.org/t/background-thread-for-periodic-updates/20786/1 "2019-02-14T13:16:06Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 14, 2019, 2:24pm UTC](https://discourse.julialang.org/t/background-thread-for-periodic-updates/20786/2 "2019-02-14T14:24:52Z")

</div>

> [@cedeerwe](#):
>
> If not, is it planned?

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

> <https://github.com/JuliaLang/julia/pull/22631>
>
> This replaces the existing fork-join threading infrastructure with a parallel ta…sk runtime (partr) that implements \[parallel depth first scheduling\](http://www.cs.cmu.edu/%7Echensm/papers/ConstructiveCacheSharing\_spaa07.pdf). This model fully supports nested parallelism.
> 
> The default remains the original threading code. Enable partr by setting \`JULIA\_PARTR := 1\` in your \`Make.user\`.
> 
> The core idea is simple -- Julia tasks can now be run by any thread. The task scheduler attempts to order task execution depth-first for provably better cache efficiency, and for true nested parallelism.
> 
> However, as tasks are an existing thing in Julia and used in a number of places, we're first introducing the infrastructure that will enable parallel tasks with this PR, keeping (hopefully) the serial semantics of the existing task interface. This PR does not introduce any new interface calls for parallel tasks -- those will be in future PRs.
> 
> All test-cases pass with \`JULIA\_PARTR\` off (as they should). With \`JULIA\_PARTR\` on, all test cases are currently passing on Linux and OS-X.
> 
> Cc: @JeffBezanson, @vtjnash, @yuyichao, @ViralBShah, @vchuravy, @anton-malakhov.

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

---

<div class="post-metadata">

### Author: ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)
#### Post date: [February 14, 2019, 3:00pm UTC](https://discourse.julialang.org/t/background-thread-for-periodic-updates/20786/3 "2019-02-14T15:00:56Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![cedeerwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cedeerwe/32/8552_2.png) [@cedeerwe](https://discourse.julialang.org/u/cedeerwe)
#### Post date: [February 14, 2019, 4:13pm UTC](https://discourse.julialang.org/t/background-thread-for-periodic-updates/20786/4 "2019-02-14T16:13:53Z")

</div>

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. 😕

> 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!
