如何在水晶石上实现图案生产者-消费者?我正在寻找这样的东西- http://ruby-doc.org/core-2.2.0/Queue.html可能我需要使用Channel,但我不明白如何..因为它是等待,而“消费者”将收到。
我的意思是:
channel = Channel(Int32).new
spawn do
15.times do |i|
# ... do something that take a time
puts "send #{i}"
channel.send i # paused while someone receive, but i want to continue do the job that takes a time..
end
end
spawn do
loop do
i = channel.receive
puts "receive #{i}"
sleep 0.5
end
end
sleep 7.5发布于 2018-01-07 05:23:50
您说得对,使用Channel是解决Crystal中并发通信的一个很好的方法。请注意,默认情况下,在接收通道之前,通道只能存储一个值。
但是您可以使用buffered Channel来向Channel发送多个值,并且不需要立即接收这些值。这本质上是一个FIFO队列,在队列的一端添加新项目,从另一端删除新项目。
# Create a channel with a buffer for 32 values
channel = Channel(Int32).new(32) https://stackoverflow.com/questions/48128133
复制相似问题