I find it difficult to grasp the idea of ββFibers \ coroutines and implementation in Crystal.
I hope this is the right place to ask about it, I completely agree with the answer "not here" :)
This is my usual way to handle multithreading 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 an incoming connection or some other thing, adding Thread to the array of threads, and then check that I have no more threads than I wanted.
What would be a good way to implement something similar in Crystal using spawn \ channels \ fiber, etc.?
source share