How to run a background thread in ruby?

南楼画角 提交于 2019-12-06 02:38:22

问题


I am new to ruby and thought it would be a great idea to rebuild a simple chat program I made in C#.

I am using Ruby 2.0.0 MRI (Matz’s Ruby Implementation).

The problem is I want to have I/O for simple server commands while the server is running. This is the server that was taken from the sample. I added the commands method that uses gets() to get input. I want this method to run as a thread in the background, but the thread is blocking the other thread.

require 'socket'                # Get sockets from stdlib

server = TCPServer.open(2000)   # Socket to listen on port 2000

def commands
    x = 1
    while x == 1
        exitProgram = gets.chomp
        if exitProgram == "exit" || exitProgram == "Exit"
            x = 2
            abort("Exiting the program.")
        end
    end
end

def main
    Thread.start(commands)
    Thread.start(server.accept) 
    loop {                          # Servers run forever

        Thread.start(server.accept) do |client|
        client.puts(Time.now.ctime) # Send the time to the client
        client.puts "Closing the connection. Bye!"
        client.close                # Disconnect from the client
      end
    }
end

main

This is the client so far.

require 'socket'      # Sockets are in standard library

hostname = 'localhost'
port = 2000

s = TCPSocket.open(hostname, port)

while line = s.gets   # Read lines from the socket
  puts line.chop      # And print with platform line terminator
end
s.close               # Close the socket when done
gets.chomp

Thanks


回答1:


Read the documentation for Thread.new (which is the same as Thread.start here)

Thread.start(commands) runs the commands method and passes its return value to a thread (which then does nothing). It's blocking because you aren't starting any threads when gets is called. You want

Thread.start { commands }

Here's a similar demo script that works just like you would expect

def commands
  while gets.strip !~ /^exit$/i
    puts "Invalid command"
  end
  abort "Exiting the program"
end

Thread.start { commands }

loop do
  puts "Type exit:"
  sleep 2
end


来源:https://stackoverflow.com/questions/25130822/how-to-run-a-background-thread-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!