# Producer consumer pattern in julia

**URL:** https://discourse.julialang.org/t/producer-consumer-pattern-in-julia/12421
**Category:** Julia at Scale
**Created:** [July 16, 2018, 3:52pm UTC](https://discourse.julialang.org/t/producer-consumer-pattern-in-julia/12421 "2018-07-16T15:52:54Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![atteson](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@atteson](https://discourse.julialang.org/u/atteson)
#### Post date: [July 16, 2018, 3:52pm UTC](https://discourse.julialang.org/t/producer-consumer-pattern-in-julia/12421/1 "2018-07-16T15:52:54Z")

</div>

I’m implementing a producer consumer pattern in julia. Each producer will be pushing things on a queue and each consumer will be poping them off. I’d like each producer and each consumer to run in a different execution thread (or process). Any thoughts on the best way to implement this? Below are my current thoughts (not yet tried):

1. It seems that the thread infrastructure in v0.6 doesn’t easily support this.
2. It seems it’s easiest to use SharedArray’s for the queue with processes for the producers and consumers.
3. Do I need to use synchronization with SharedArray’s? I assume push! and pull! are not atomic. I see that julia has synchronization primitives but they seem to be for threads rather than proceses.

---

<div class="post-metadata">

### Author: ![Jordan\_Cluts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jordan_cluts/32/13753_2.png) [@Jordan\_Cluts](https://discourse.julialang.org/u/Jordan_Cluts)
#### Post date: [July 16, 2018, 4:05pm UTC](https://discourse.julialang.org/t/producer-consumer-pattern-in-julia/12421/2 "2018-07-16T16:05:37Z")

</div>

I could be very wrong but I think that [Remote Channels](https://docs.julialang.org/en/stable/manual/parallel-computing/#Channels-and-RemoteChannels-1) are intended to handle this type of thing.

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [July 17, 2018, 3:38am UTC](https://discourse.julialang.org/t/producer-consumer-pattern-in-julia/12421/3 "2018-07-17T03:38:51Z")

</div>

Keep in mind that SharedArray only works on the same server. If you need to parallelize the work across multiple servers you will pay the price of transmitting data.

I recently did something similar utilizing ZMQ.jl PUSH/PULL sockets. The producer pushes requests and the consumers receive them in a round robin fashion. That works quite well and it’s just several lines of code to hook things up.
