This commit is contained in:
Loic Nageleisen 2015-08-03 13:50:05 +02:00
parent 1921f98e47
commit 5a968ac76c
11 changed files with 219 additions and 0 deletions

View file

@ -0,0 +1,11 @@
# https://gobyexample.com/channel_buffering
require 'channel'
messages = Channel.new(2)
messages << 'buffered'
messages << 'channel'
puts messages.recv
puts messages.recv

View file

@ -0,0 +1,18 @@
# https://gobyexample.com/channel-directions
require 'channel'
def ping(pings, msg)
pings << msg
end
def pong(pings, pongs)
msg = pings.recv
pongs << msg
end
pings = Channel.new(1)
pongs = Channel.new(1)
ping(pings, 'passed message')
pong(pings, pongs)
puts pongs.recv

View file

@ -0,0 +1,15 @@
# https://gobyexample.com/channel-synchronization
require 'channel'
def worker(done)
puts 'working...'
sleep 1
puts 'done'
done << true
end
done = Channel.new(1)
go -> { worker(done) }
done.recv

10
examples/channels.rb Normal file
View file

@ -0,0 +1,10 @@
# https://gobyexample.com/channels
require 'channel'
messages = Channel.new
go -> { messages << 'ping' }
msg = messages.recv
puts msg

View file

@ -0,0 +1,31 @@
# https://gobyexample.com/closing-channels
require 'channel'
jobs = Channel.new(5)
done = Channel.new
go lambda {
loop do
begin
j = jobs.recv
rescue Channel::Closed
# TODO: wrong! ends before all items recv'd
# j, more := <-jobs; more == True
puts 'received all jobs'
done << true
return
else
puts "received job #{j}"
end
end
}
3.times do |j|
jobs << j
puts "sent job #{j}"
end
jobs.close
puts 'sent all jobs'
done.recv

15
examples/many_to_one.rb Normal file
View file

@ -0,0 +1,15 @@
# http://stackoverflow.com/questions/15715605/multiple-goroutines-listening-on-one-channel
require 'channel'
c = Channel.new
1.upto(5) do |i|
go(lambda { |i, co|
1.upto(5) do |j|
co << format("hi from %d.%d", i, j)
end
}, i, c)
end
25.times { puts c.recv }

View file

@ -0,0 +1,30 @@
# https://gobyexample.com/non-blocking-channel-operations
require 'channel'
messages = Channel.new
signals = Channel.new
Channel.select(messages) do |msg, c|
case c
when messages then puts "received message #{msg}"
else puts 'no message received'
end
end
msg = 'hi'
messages <- msg
#select {
#case messages <- msg:
# fmt.Println("sent message", msg)
#default:
# fmt.Println("no message sent")
#}
Channel.select(messages, signals) do |res, c|
case c
when messages then puts "received message #{res}"
when signals then puts "received signal #{res}"
else puts 'no activity'
end
end

24
examples/one_to_many.rb Normal file
View file

@ -0,0 +1,24 @@
# http://stackoverflow.com/questions/15715605/multiple-goroutines-listening-on-one-channel
require 'channel'
require 'channel/waitgroup'
c = Channel.new
w = WaitGroup.new
w.add(5)
1.upto(5) do |i|
go(lambda { |i, ci|
j = 1
ci.each { |v|
sleep(0.001)
puts format("%d.%d got %d", i, j, v)
j += 1
}
w.done
}, i, c)
end
1.upto(25) { |i| c << i }
c.close
w.wait

View file

@ -0,0 +1,10 @@
# https://gobyexample.com/range-over-channels
require 'channel'
queue = Channel.new(2)
queue << 'one'
queue << 'two'
close(queue)
queue.each { |e| puts e }

25
examples/select.rb Normal file
View file

@ -0,0 +1,25 @@
# https://gobyexample.com/select
require 'channel'
c1 = Channel.new
c2 = Channel.new
go lambda {
sleep(1)
c1 << 'one'
}
go lambda {
sleep(2)
c2 << 'two'
}
2.times do
Channel.select(c1, c2) do |msg, c|
case c
when c1 then puts "received #{msg}"
when c2 then puts "received #{msg}"
end
end
end

30
examples/timeout.rb Normal file
View file

@ -0,0 +1,30 @@
# https://gobyexample.com/timeouts
require 'channel'
require 'channel/timeout'
c1 = Channel.new(1)
go lambda {
sleep(2)
c1 << 'result 1'
}
Channel.select(c1, t1 = Channel::Timeout.after(1)) do |res, c|
case c
when c1 then puts res
when t1 then puts 'timeout 1'
end
end
c2 = Channel.new(1)
go lambda {
sleep(2)
c2 << 'result 2'
}
Channel.select(c2, t2 = Channel::Timeout.after(3)) do |res, c|
case c
when c2 then puts res
when t2 then puts 'timeout 1'
end
end