https://github.com/statusfailed/machines-io
https://github.com/statusfailed/machines-io
Last synced: 12 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/statusfailed/machines-io
- Owner: statusfailed
- License: mit
- Created: 2015-04-26T18:42:31.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2018-01-30T23:07:40.000Z (about 8 years ago)
- Last Synced: 2025-01-31T08:18:58.369Z (about 1 year ago)
- Language: Haskell
- Size: 6.84 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Concurrency utilities for `machines`
NOTE: this is mostly an experiment. I advise against using it!
# What is it for?
Simple use case: if you have two sources of data (say sockets),
and you want to do a blocking read on both, and merge the events
Example code below. Note this will finish by crashing, because
there are no more processes writing to the `Chan`.
{-# LANGUAGE RankNTypes #-}
module Main where
import Data.Machine
import Data.Machine.IO.Concurrent
import Control.Monad.IO.Class
import Control.Concurrent (newChan, forkIO, threadDelay)
-- | Yield elements from a list with a specified delay in microseconds between each.
slowSource :: MonadIO m => Int -> [a] -> ProcessT m () a
slowSource t xs = construct $ go xs
where
go [] = return ()
go (x:xs) = do
liftIO $ threadDelay t -- wait 1 second
yield x
go xs
main = do
c <- newChan
t1 <- forkIO id (runT_ $ slowSource 500000 [1..4] ~> toChan c)
t2 <- forkIO id (runT_ $ slowSource 1000000 [1..4] ~> toChan c)
runT_ $ fromChan c ~> autoM print
print "done"
Running it:
*Main> main
1
2
1
3
4
2
3
4
*** Exception: thread blocked indefinitely in an MVar operation