# Best way for a task to read from two channels

**URL:** https://discourse.julialang.org/t/best-way-for-a-task-to-read-from-two-channels/68704
**Category:** General Usage
**Tags:** parallel, task, channel
**Created:** [September 24, 2021, 3:04pm UTC](https://discourse.julialang.org/t/best-way-for-a-task-to-read-from-two-channels/68704 "2021-09-24T15:04:00Z")
**Posts on this page:** 6
**Page:** 2

<div class="post-metadata">

### Author: ![adriano.vilela](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adriano.vilela/32/7596_2.png) [@adriano.vilela](https://discourse.julialang.org/u/adriano.vilela)
#### Post date: [September 26, 2021, 9:52pm UTC](https://discourse.julialang.org/t/best-way-for-a-task-to-read-from-two-channels/68704/21 "2021-09-26T21:52:43Z")

</div>

Sure. I think I’m already doing that. I’m sending time stamps in both channels:

Video frames channel: `(video_frame, source_camera, time_stamp)`

Detection channel: `(detection_status, source_camera, time_stamp)`

Right now I’m merging these two tuples into a single one and sending it along the merged channel:

`((video_frame, source_camera, time_stamp), (detection_status, source_camera, time_stamp))`

The time stamps accompanying each frame are acting as serial numbers.

---

<div class="post-metadata">

### Author: ![adriano.vilela](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adriano.vilela/32/7596_2.png) [@adriano.vilela](https://discourse.julialang.org/u/adriano.vilela)
#### Post date: [September 26, 2021, 9:58pm UTC](https://discourse.julialang.org/t/best-way-for-a-task-to-read-from-two-channels/68704/22 "2021-09-26T21:58:03Z")

</div>

> [@tkf](#):
>
> Maybe using something like `Channel{Union{PushFrame,DetectionNotification}}` where `PushFrame` and `DetectionNotification` are appropriate `struct` .

Now, that’s really interesting, @tkf.

Maybe I could use multiple dispatch to define two inner methods in the `buffer_frames()` function to process the data taken from the merged channel?

```julia
function buffer_frames()

	function do_work(data::PushFrame)
	
		# Add 'data' (a video frame) to the circular buffer
		
		...
	
	end

	function do_work(data::DetectionNotification)
	
		# Use the frame buffer to create a video on disk
	
	
	end

	for data in incoming_channel

		do_work(data)

	end

end

```

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [September 26, 2021, 10:30pm UTC](https://discourse.julialang.org/t/best-way-for-a-task-to-read-from-two-channels/68704/23 "2021-09-26T22:30:27Z")

</div>

Yes, multiple dispatch works pretty well for this. You can also do simple `if _ isa _` dispatch:

```julia
for data in incoming_channel
    if data isa PushFrame
        ...
    else
        @assert data isa DetectionNotification
        ...
    end
end

```

Both approaches are supported by the compiler very well.

---

<div class="post-metadata">

### Author: ![adriano.vilela](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adriano.vilela/32/7596_2.png) [@adriano.vilela](https://discourse.julialang.org/u/adriano.vilela)
#### Post date: [September 27, 2021, 12:22am UTC](https://discourse.julialang.org/t/best-way-for-a-task-to-read-from-two-channels/68704/24 "2021-09-27T00:22:05Z")

</div>

I thought of using `if isa ...` as well, but I thought that was considered bad programming practice. It certainly reminds me from my old Python days…

---

<div class="post-metadata">

### Author: ![adriano.vilela](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adriano.vilela/32/7596_2.png) [@adriano.vilela](https://discourse.julialang.org/u/adriano.vilela)
#### Post date: [September 27, 2021, 12:31am UTC](https://discourse.julialang.org/t/best-way-for-a-task-to-read-from-two-channels/68704/25 "2021-09-27T00:31:31Z")

</div>

Hi @tkf,

Just out of curiosity: could [FLoops.jl](https://github.com/JuliaFolds/FLoops.jl) help me with any of this or is it meant specifically for data parallelism problems?

Thanks a lot for all your help with this.

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [September 27, 2021, 12:48am UTC](https://discourse.julialang.org/t/best-way-for-a-task-to-read-from-two-channels/68704/26 "2021-09-27T00:48:30Z")

</div>

Yes, FLoops.jl is for data parallelism (plus sequential loops). FLoops.jl doesn’t help much if you are juggling channels.

[Previous page](https://discourse.julialang.org/t/best-way-for-a-task-to-read-from-two-channels/68704.md?page=1)
