[英]pipes and fork with Control.Concurrent.MonadIO
In the following code I am trying to combine 2 Producer
s into 1. All have the same type. 在下面的代码中,我试图将2个
Producer
s组合成1.所有类型都相同。 They will be combined by each of the 2 input Producer
s being run in a separate thread and being consumed by a Consumer
that puts the values into an unagi chan (I'm using unagi chan for performance). 它们将由两个输入
Producer
的每一个组合在一个单独的线程中运行,并由Consumer
,将Consumer
放入一个unagi chan(我使用unagi chan来提高性能)。 A producer is returned which reads from the chan. 返回一个从chan读取的生产者。 I want to be able to take any
Producer
as long as it can run IO
so I restrict the class of the monad to MonadIO
and HasFork
. 我希望能够使用任何
Producer
,只要它可以运行IO
所以我将monad的类限制为MonadIO
和HasFork
。
import Pipes (Producer, Consumer, (>->), yield, await, runEffect)
import Control.Concurrent.Chan.Unagi (InChan, OutChan, newChan, readChan, writeChan)
import Control.Monad (forever, void)
import Control.Concurrent.MonadIO (MonadIO, HasFork, liftIO, fork)
combine :: (MonadIO m, HasFork m) => Producer a m r -> Producer a m r -> Producer a m r
combine p1 p2 = do
(inChan, outChan) <- liftIO $ newChan
t1 <- fork . void . runEffect $ p1 >-> (consumer inChan)
t2 <- fork . void . runEffect $ p2 >-> (consumer inChan)
producer outChan
producer :: (MonadIO m) => OutChan a -> Producer a m r
producer outChan = forever $ do
msg <- liftIO $ readChan outChan
yield msg
consumer :: (MonadIO m) => InChan a -> Consumer a m r
consumer inChan = forever $ do
msg <- await
liftIO $ writeChan inChan msg
However I get the following error: 但是我收到以下错误:
• Couldn't match type ‘m’
with ‘Pipes.Internal.Proxy Pipes.Internal.X () () a m’
‘m’ is a rigid type variable bound by
the type signature for:
combine :: forall (m :: * -> *) a r.
(MonadIO m, HasFork m) =>
Producer a m r -> Producer a m r -> Producer a m r
at src/Pipes/Unagi.hs:8:12
Expected type: Pipes.Internal.Proxy
Pipes.Internal.X () () a m GHC.Conc.Sync.ThreadId
Actual type: m GHC.Conc.Sync.ThreadId
Is what I'm trying to do possible? 我正在尝试做什么?
Add lift
in front of fork
. 在
fork
添加lift
。
t1 <- lift . fork . ...
In your function, fork . ...
在你的函数中,
fork . ...
fork . ...
has type m ThreadId
, but the do
block is in the monad Producer am
. fork . ...
有类型m ThreadId
,但是do
块在monad Producer am
。
Also, lifted-base
has a more up to date fork
. 此外,
lifted-base
有一个更新的fork
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.