Search code examples
multithreadingcoroutinecrystal-lang

Crystal convert the idea behind Thread pool to Fibers/spawn


I'm having some hard time learning the idea behind Fibers\coroutines and the implementation in Crystal.

I hope this is the right place to ask this, I'll totally accept a "not here" answer :)

This is my usual way of handling multi-threading in Ruby:

threads = []
max_threads = 10

loop do
  begin
    threads << Thread.new do
      helper_method(1,2,3,4)
    end
  rescue Exception => e
    puts "Error Starting thread"
  end

  begin
    threads = threads.select { |t| t.alive? ? true : (t.join; false) }
    while threads.size >= max_threads
      puts 'Got Maximum threads'
      sleep 1
      threads = threads.select { |t| t.alive? ? true : (t.join; false) }
    end
  rescue Exception => e
    puts e
  end
end

This way I open a new Thread, usually of a incoming connection or some other thing, add the Thread to a threads array, and then check that I don't have more threads then what I wanted.

What would be a good way to implement something similar in Crystal using spawn\channels\fibers etc.. ?


Solution

  • Something like this:

    require "socket"
    
    ch = Channel(TCPSocket).new
    
    10.times do
      spawn do
        loop do
          socket = ch.receive
          socket.puts "Hi!"
          socket.close
        end
      end
    end
    
    server = TCPServer.new(1234)
    loop do
      socket = server.accept
      ch.send socket
    end
    

    This code will pre-spawn 10 fibers to attend the requests. The channel is unbuffered so the connections wont be queuing if they cannot be attended by any fiber.