Signals in Crystal
Here’s the translation of the Go code to Crystal, formatted in Markdown suitable for Hugo:
Our first example demonstrates how to handle Unix signals in Crystal. This is useful for scenarios like gracefully shutting down a server when it receives a SIGTERM
, or stopping a command-line tool from processing input when it receives a SIGINT
.
When we run this program, it will block waiting for a signal. By typing ctrl-C
(which the terminal shows as ^C
), we can send a SIGINT
signal, causing the program to print SIGINT
and then exit.
In Crystal, we use the Signal
module to handle Unix signals. Instead of using a buffered channel like in the original example, we use Crystal’s trap
method to set up signal handlers. These handlers send the received signals to a channel.
We use fibers (Crystal’s lightweight threads) instead of goroutines, but the concept is similar. The main fiber waits for a value on the done
channel, which is sent when a signal is received.
This example demonstrates Crystal’s concurrency model using channels and fibers, as well as its signal handling capabilities.