Last active
August 7, 2021 15:36
-
-
Save spikeheap/61a729377896c6c9685e58d345ceb72e to your computer and use it in GitHub Desktop.
Reading STDIN with threads in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'thread' | |
queue = Queue.new | |
producer = Thread.new do | |
loop do | |
key = STDIN.getch | |
queue << key if key | |
end | |
end | |
consumer = Thread.new do | |
loop do | |
key = queue.pop | |
# Pusher code here | |
# ctrl-c | |
exit if key == "\u0003" | |
end | |
end | |
consumer.join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment