# @spawn and @async in producer-consumer problem

**URL:** <https://discourse.julialang.org/t/spawn-and-async-in-producer-consumer-problem/91252>\
**Category:** Performance\
**Tags:** multithreading\
**Created:** [December 5, 2022, 10:21am UTC](https://discourse.julialang.org/t/spawn-and-async-in-producer-consumer-problem/91252 "2022-12-05T10:21:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![achjaj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/achjaj/32/22358_2.png) [@achjaj](https://discourse.julialang.org/u/achjaj)\
**Post date:** [December 5, 2022, 10:21am UTC](https://discourse.julialang.org/t/spawn-and-async-in-producer-consumer-problem/91252/1 "2022-12-05T10:21:28Z")

</div>

Hello,

I am trying to implement a producer-consumer problem, where I want one thread to produce some data and a second thread to take the data and write them to a disk. I am using `Channel` as my queue so that the producer will `put!` the data to the channel and the consumer will `take` them. I can run the consumer on the main thread, but I am not sure how I should create the producer thread. I do not understand the difference between `Threads.@spawn` and `Threads.@async`.

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [December 5, 2022, 11:23am UTC](https://discourse.julialang.org/t/spawn-and-async-in-producer-consumer-problem/91252/2 "2022-12-05T11:23:28Z")

</div>

As far as I can tell, the “only” (but potentially important) difference is, that `@async` sets the sticky bit on the created task, whereas `@spawn` does not (or rather, removes it, because it is set by default on newly created tasks). The sticky bit signals that a task is to be executed on the thread that schedules it only. A non-sticky task can be freely scheduled and moved around available threads.

Chances are you want `@spawn`.

use `@macroexpand` to see what each of `@async, @spawn` does.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [December 5, 2022, 12:24pm UTC](https://discourse.julialang.org/t/spawn-and-async-in-producer-consumer-problem/91252/3 "2022-12-05T12:24:32Z")

</div>

And with the task moving around between threads comes the whole class of thread-safety problems. So for things where you don’t want to worry about thread-safety, but just want IO to happen without blocking, `@async` is a good default.

---

<div class="post-metadata">

**Author:** ![achjaj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/achjaj/32/22358_2.png) [@achjaj](https://discourse.julialang.org/u/achjaj)\
**Post date:** [December 5, 2022, 2:03pm UTC](https://discourse.julialang.org/t/spawn-and-async-in-producer-consumer-problem/91252/4 "2022-12-05T14:03:23Z")

</div>

There should be no problem with thread-safety. The only data that the consumer will access are the final data from the producer. The communication between these two is handled with `Channel`, so thread-safety should be guaranteed.

---

<div class="post-metadata">

**Author:** ![achjaj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/achjaj/32/22358_2.png) [@achjaj](https://discourse.julialang.org/u/achjaj)\
**Post date:** [December 5, 2022, 2:09pm UTC](https://discourse.julialang.org/t/spawn-and-async-in-producer-consumer-problem/91252/5 "2022-12-05T14:09:37Z")

</div>

> use `@macroexpand` to see what each of `@async, @spawn` does.

~~How can I do that? When I try to execute `@macroexpand Threads.@async` I got an error because REPL is trying to execute the `@async` macro.~~

**EDIT:**  
Ok, I figured it out, I just need to do something like `@macroexpand Threads.@async (x) -> x`, for example.
