Skip to content

towski/channels

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Implement Go style channels in ruby, asynchronous threads that can pass messages through a blocking channel.

See Rob Pike's go talk: http://www.youtube.com/watch?v=f6kdp27TYZs

Simple example:

require 'channel'
# wait for input to the channel in a separate thread
channel = Channel.new{|c| loop { puts c.read } }
# send a message from the main thread
channel.write "I've... seen things you people wouldn't believe..."
sleep 2
channel.write "Attack ships on fire off the shoulder of Orion."
sleep 3
channel.write "I watched c-beams glitter in the dark near the Tannhauser Gate."
sleep 4
channel.write "All those... moments... will be lost in time, like... *cough*"
sleep 5
channel.write "tears... in... rain."
sleep 2
channel.write "Time... to die..."
sleep 2

In the following example, each separate channel is writing to a main channel whenever it receives a response, and the main channel pops the responses off as soon as it receives them.

require 'channel'
require 'net/http'

Channel.new do |main|
  Channel.new { main.write Net::HTTP.get('indietorrents.com', '/index.html') }
  Channel.new { main.write Net::HTTP.get('google.com', '/index.html') }
  Channel.new { main.write Net::HTTP.get('yelp.com', '/index.html') }
  loop do
    # outputs the response data in the order it is received
    puts main.read
  end
end

sleep

This can be simplified by using subchannels.

class Display < Channel
  def run
    # display loop
  end
end

class Main < Channel
  def initialize
    @display = Display.new
    # handle any messages from the display channel
    add_subchannel(@display){|response, sender|
      if response == "quit"
        puts "it's ova"
        exit
      end
    }
  end
end

See https://github.com/towski/dwarf for more examples

About

Go-style channels in ruby

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages