Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #! /usr/bin/env ruby
  2.  
  3. #$ ruby better_select.rb
  4. #=> read2
  5. #=> end
  6.  
  7. require 'thread'
  8.  
  9. def better_select(reads, writes={}, excepts={}, timeout=nil)
  10. rs, ws, es = IO.select(reads.keys, writes.keys, excepts.keys, timeout)
  11. rs.each do |io|
  12. reads[io].call(io)
  13. end
  14. ws.each do |io|
  15. writes[io].call(io)
  16. end
  17. es.each do |io|
  18. excepts[io].call(io)
  19. end
  20. end
  21.  
  22. q = Queue.new
  23. r, w = IO.pipe
  24. r2, w2 = IO.pipe
  25. Thread.new {
  26. better_select({
  27. r => ->(io) {
  28. puts "read1"
  29. q.push "end"
  30. },
  31. r2 => ->(io) {
  32. puts "read2"
  33. q.push "end"
  34. }
  35. })
  36. }.abort_on_exception = true
  37.  
  38. w2.write("GO")
  39. w2.close
  40.  
  41. puts q.pop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement