How to run a Haskell program endlessly using only Haskell?

I have a small program that should run every 5 minutes.

I currently have a script shell that performs this task, but I want to give the user the ability to run it without additional scripts using a key in the CLI.

What is the best way to achieve this?

+7
haskell infinite-loop periodic-task periodic-processing
source share
1 answer

I assume you want something similar (more or less pseudo-code):

import Control.Concurrent (forkIO, threadDelay) import Data.IORef import Control.Monad (forever) main = do var <- newIORef 5000 forkIO (forever $ process var) forever $ readInput var process var = do doActualProcessing interval <- readIORef var _ <- threadDelay interval readInput var = do newInterval <- readLn writeIORef var newInterval 

If you need to transfer more complex data from an input stream to a processing stream, MVar or TVar may be a better choice than IORef s.

+9
source share

All Articles