I am using "pipes-concurrency" trying to model the following scenario: my "source" pipe has type of
stream :: Producer InputData (StateT References IO) ()
where "References" is just a map that I accumulate while streaming the source. Then I am following the "broadcast" example from the Tutorial attempting to "fork" my flow into two branches:
main = do
(output1, input1) <- spawn unbounded
(output2, input2) <- spawn unbounded
a1 <- async $ do
execStateT (runEffect $ stream >-> toOutput (output1 <> output2)) emptyTables
a2 <- async $ do
withFile "/tmp/geo/smart-grouping.xml" WriteMode $ \h -> do
evalStateT (runEffect $ groupSmart (fromInput input1) >-> toHandle h) ??? -- what to put here?
a3 <- async $ do
withFile "/tmp/geo/damnGrouping.xml" WriteMode $ \h -> do
evalStateT (runEffect $ groupDamn (fromInput input2) >-> toHandle h) ???
mapM_ wait [a1,a2,a3]
I would like my branches to use the information from the source's StateT, however following the types, it looks like I have to provide each branch with the initial (empty?) state.
How do I share the state between the source and the forked branches? Or how do I model this situation correctly?
The general rule of thumb is that you should only use concurrency for two reasons:
* Increased performance (and only if the increase offsets the context switching)
* Waiting on multiple concurrent input streams
Generally you should try to avoid using concurrency just as a control flow mechanism and instead you should try to use pure, single-threaded ways of forking input streams. The main reason you want to avoid concurrency unless absolutely necessary is that it's very difficult to test and reason about concurrent code.
The answer to this question depends on how `groupSmart` and `groupDamn` are implemented. However, I can give one example if I can make certain assumptions.
Let's assume for simplicity that `groupSmart`/`groupDamn` emit one output element for every input element. In that case you can actually encode them as `Fold`s from my `foldl` library:
groupSmart :: Fold I O1
groupDamn :: Fold I O2
Then if you wanted to run both grouping mechanisms in parallel over the same stream, you would just combine them using `Applicative` syntax:
groupBoth :: Fold I (O1, O2)
groupBoth = (,) <$> groupSmart <*> groupDamn
Then you would transform that into a `Pipe` by using:
import Control.Foldl (purely)
import qualified Pipes.Prelude as Pipes
pipeBoth :: Monad m => Pipe I (O1, O2) m r
pipeBoth = purely Pipes.scan groupBoth
Then you would just write something like:
example :: MonadIO io => Consumer I io r
example = for (purely Pipes.scan groupBoth) (\(o1, o2) -> do
liftIO (writeToHandle handle1 o1)
liftIO (writeToHandle handle2 o2) )
... and now you can do everything within a single pipeline.
You can also give the `groupSmart`/`groupDamn` folds access to the `StateT` layer by generalizing them to `FoldM`s instead:
groupSmart :: FoldM (StateT References IO) I O1
groupDamn :: FoldM (StateT References IO) I O1
... and the only change you would make is to use `impurely Pipes.scanM` instead of `purely Pipes.scan`.
On 8/23/2015 5:31 AM, Alexey Raga wrote:
You are right, I don't need any concurrency, I just wanted to use it for the ability to broadcast producers.
I actually was looking at Fold, but I couldn't figure out how it could work for the following requirements :
- I need to group elements differently in each branch. I wouldn't be able to use pipes-groups here because it gives me functions of Producers, not Pipes.
In fact what I wanted is to have a stream, gather some stats on it (here is my StateT), group it in two different ways and output both results into different files.
I will have a look at Fold again, would it still be right tool for the job with the requirements I mentioned?
I think you might end up having to do the two `Fold`s which each return a `Maybe`. I don't know a more elegant way of decomposing the problem.
On 8/23/2015 3:51 PM, Alexey Raga wrote:
In reply to this post by Alexey Raga
> I need to group elements differently in each branch. I wouldn't be able to use pipes-groups here because it gives me functions of Producers, not Pipes.
> - due to grouping my branches would emit at different rates and not for every input element.
I have uploaded foldl-transduce to Hackage. It lets you perform grouping operations fold-side instead of producer-side, which means you can still combine the resulting folds with <*> :
import qualified Control.Foldl as L
L.fold ((,) <$> (folds (chunksOf 2) L.list L.list) <*> (folds (chunksOf 3) L.list L.list)) [1..7]
Here's a monadic example:
import qualified Data.ByteString as B
toHandle :: Handle -> FoldM IO B.ByteString ()
toHandle h = FoldM (\_ -> B.hPut h) (pure ()) (\_ -> pure ())
testGroupIO :: IO ()
withFile "file1" AppendMode $ \h1 ->
withFile "file2" AppendMode $ \h2 ->
flip L.foldM (replicate 10 "aabbccddeeffgghhiijj") $
foldsM (chunksOf 7) (L.generalize L.list) (L.handlesM traverse (toHandle h1)) *>
foldsM (chunksOf 3) (L.generalize L.list) (L.handlesM traverse (toHandle h2))
Could this help you with your use case?
On Monday, August 24, 2015 at 12:51:16 AM UTC+2, Alexey Raga wrote:
Thank you very much, I will definitely give it a try!
On Tuesday, August 25, 2015 at 4:29:41 PM UTC+10, Daniel Díaz wrote:
|Free forum by Nabble||Edit this page|