Core.async patterns poison pill vs close!


#1

The poison pill video shows putting a special value on a channel to stop a worker. As mentioned in the video, this can only work when there’s a single consumer taking from the channel.

I’m wondering why you would use this rather than just closing the channel and using if-some to read items.

(go
  (loop []
    (if-some [v (<! work)]
      (do-work v)
      (recur))))

The only reason I can think of for using a poison pill is if you want to communicate a reason for stopping to the consumer.

Is there another reason I missed?


#2

Hi @david,

Thanks for your question!

Yes, the poison pill pattern is appropriate if you can’t use the closing of a channel to communicate stopping working. In simple cases, I’ll just close the channel like you show.

In more complex cases, the poison pill may be better. To kill one worker, you send one poison pill. To kill two, you send two.

Rock on!
Eric