I’ve been reading https://crystal-lang.org/reference/guides/concurrency.html and everything makes sense and it’s well-written. Unfortunately, when I got to the
Channels section, my mind has exploded.
For example, this TCP Server handles multiple connections easily:
require "socket" def handle_client(client) message = client.gets client.puts message end server = TCPServer.new("localhost", 1234) while client = server.accept? spawn handle_client(client) end
Compare that to:
require "socket" channel = Channel(String).new spawn do server = TCPServer.new("0.0.0.0", 8080) socket = server.accept while line = socket.gets channel.send(line) end end spawn do while line = gets channel.send(line) end end 3.times do puts channel.receive end
What exactly is happening when something is “sent through a channel” as compared to “spawned in a fiber”?