Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a background thread in ruby?

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
like image 656
user3081457 Avatar asked Aug 05 '14 03:08

user3081457


People also ask

How do you start a thread in Ruby?

In order to create new threads, Ruby provides ::new , ::start , and ::fork . A block must be provided with each of these methods, otherwise a ThreadError will be raised. When subclassing the Thread class, the initialize method of your subclass will be ignored by ::start and ::fork .

How many threads can I run Ruby?

The Ruby interpreter handles the management of the threads and only one or two native thread are created.

Is ROR multithreaded?

Rails as a framework is thread-safe. So, the answer is yes!

Does Ruby support multithreading?

Multi-threading is the most useful property of Ruby which allows concurrent programming of two or more parts of the program for maximizing the utilization of CPU. Each part of a program is called Thread. So, in other words, threads are lightweight processes within a process.


1 Answers

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
like image 94
Max Avatar answered Oct 19 '22 07:10

Max