Multithreading 它有水晶郎队列吗?
如何在crystal lang上实现图案生产者-消费者?我在找这样的东西- 可能我需要使用Multithreading 它有水晶郎队列吗?,multithreading,queue,crystal-lang,Multithreading,Queue,Crystal Lang,如何在crystal lang上实现图案生产者-消费者?我在找这样的东西- 可能我需要使用频道,但我不知道如何。。因为这是等待,而“消费者”将收到 我的意思是: 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,
频道
,但我不知道如何。。因为这是等待,而“消费者”将收到
我的意思是:
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
你说得对,使用a是解决Crystal中Connccurent通信的一个很好的方法。
请注意,默认情况下,通道在收到之前只能存储一个值
但是,您可以使用缓冲通道向通道发送多个值,而不需要立即接收这些值。这本质上是一个FIFO队列,其中新项目在一端添加,从另一端删除
# Create a channel with a buffer for 32 values
channel = Channel(Int32).new(32)
你真棒!。非常感谢。