Signals in Idris
Here’s the translation of the Go code to Idris, formatted in Markdown suitable for Hugo:
Our first example demonstrates how to handle signals in Idris. This can be useful for gracefully shutting down a server when it receives a SIGTERM
, or stopping a command-line tool from processing input when it receives a SIGINT
.
In this Idris program:
We create a channel
sigChan
to receive signal notifications.We use
registerSignalHandler
to registerSIGINT
andSIGTERM
signals. When these signals are received, they’re sent tosigChan
.We create another channel
doneChan
to coordinate between the main thread and the signal handling thread.We start a separate thread that waits for a signal on
sigChan
. When it receives a signal, it prints the signal and notifies the main thread viadoneChan
.The main thread waits on
doneChan
, effectively blocking until a signal is received and processed.
To run this program:
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 the received signal and then exit.
Note that Idris’s approach to concurrency and signal handling is different from some other languages. It uses lightweight threads and channels for communication, which provides a safe and composable way to handle concurrent operations.