frp - How do I model time-based events in haskell? -
i'm building queue multiplexer in haskell, partly way learn , partly replacement dodgy shell scripts.
what periodically connect each queue (we're using rabbitmq) in turn measure queue depth. if main system consuming queue , queue has messages, nothing. if connected queue empty switch queue messages on it. if queues empty nothing. far good.
i need time-based switch, such if main application has been connected queue more 1 hour , queue empty, switch if other queues empty.
i'm thinking want emits events periodically trigger next cycle of behavior, 'check' events every minute , 'time-switch' events every hour.
is there idiomatic way model time-based events in haskell?
if understand correctly, sounds job control.concurrent
. parts you'd use forkio
spawn 1 of haskell's lightweight threads handle timing loop, , chan
receive timing signals.
import control.concurrent import control.concurrent.chan import control.monad (forever) main = -- channel receive messages timing thread chan <- newchan :: io (chan string) -- spawn new thread send periodic signals forkio $ forever $ writechan chan "hi!" threaddelay $ 1 * 1000 * 1000 -- delay in microseconds -- in main thread, listen messages chan. forever $ readchan chan >>= print
to expand on this, instead of writing string
s chan
make adt events, like:
data event = checkevent | switchevent deriving show
and have listening thread different things based on event type.
Comments
Post a Comment