[Haskell-cafe] Help with async library and killing child processes
Patrick Chilton
chpatrick at gmail.com
Wed Mar 18 00:48:31 UTC 2020
Hi, I think you're looking for Concurrently:
https://hackage.haskell.org/package/async-2.2.2/docs/Control-Concurrent-Async.html#t:Concurrently
On Wed, Mar 18, 2020 at 4:52 AM Michael Hull <mikehulluk at gmail.com> wrote:
> Hi,
> I am currently learning Haskell, and really enjoying it, thankyou to the
> community for this well considered language.
> I have started using the async library, and I can't quite figure out some
> of the documentation, and I wonder if someone could help.
>
>
> https://hackage.haskell.org/package/async-2.2.2/docs/Control-Concurrent-Async.html#g:3
>
> Makes the comment that: "This is a useful variant of async
> <https://hackage.haskell.org/package/async-2.2.2/docs/Control-Concurrent-Async.html#v:async> that
> ensures an Async is never left running unintentionally." However, I can't
> see how I would use this.
>
> To make the example more concrete, I have a function that looks like this:
>
>
> runNodeHwBridgeUdp :: NodeHwInterface -> NodeHwUdpConfig-> IO()
> runNodeHwBridgeUdp (NodeHwInterface l0up l0down) config = do
> putStrLn "Starting Node bridge"
>
> -- Launch all the subprocesses:
> t0 <- async $ (runMsgUpReader hwBridgeSharedData l0up)
> t1 <- async $ (evalStateT (runUDPWriter hwBridgeSharedData)
> (initialUdpDispatchData timeUs) )
> t2 <- async $ (runUDPReader hwBridgeSharedData l0down)
>
> putStrLn "Created child processes. Waiting...."
> (a, is_exception) <- waitAnyCatchCancel [t0, t1, t2]
>
> case is_exception of
> Right _ -> do
> error "SHOULD NOT GET HERE"
> Left exception -> do
> case (fromException exception) of
> Just (ex :: HwDestroyException) -> do
> putStrLn $ "BRIDGE ENDING: >>>> GOOD: SHUTDOWN: " ++ show
> exception
> Nothing -> do
> throwIO exception
> return ()
>
>
> This process launches 3 child processes, and then waits on them. I launch
> this function as an async-process, and it would be really helpful if when I
> "cancel"ed it, it would also also cancelled the children, but I don't
> understand how I would structure that...
>
> t0 <- withAsync $ (runMsgUpReader hwBridgeSharedData l0up) $ \async ->
> do
> ????
> t1 <- withAsync $ (evalStateT (runUDPWriter hwBridgeSharedData)
> (initialUdpDispatchData timeUs) ) $ \async -> do
> ????
> t2 <- async $ (runUDPReader hwBridgeSharedData l0down) $ \async -> do
> ????
>
> I feel like I must be missing something here.
> I'd appreciate any input; many thanks for your time,
>
>
>
> Mike
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20200318/55f8f865/attachment.html>
More information about the Haskell-Cafe
mailing list