[Haskell-cafe] Wrapping external process communication in a nice API?

Tristan Cacqueray tdecacqu at redhat.com
Mon May 31 18:08:47 UTC 2021

On Mon, May 31, 2021 at 19:03 Mario Lang wrote:
> Tristan Cacqueray <tdecacqu at redhat.com> writes:
>> On Mon, May 31, 2021 at 17:31 Mario Lang wrote:
>>> Tristan Cacqueray <tdecacqu at redhat.com> writes:
>>>> It seems like it would be safer to use withCreateProcess instead of
>>>> createProcess to ensure the process is not left unattended.
>>> Maybe I am unimaginative, but it seems to me I can not use
>>> withCreateProcess as I need to read/write the Handles outside of the
>>> initialisation phase.  It seems to me withCreateProcess would kill my
>>> external process once my initialisation function is done.
>>> Or do I misunderstand bracket somehow?
>>> AIUI, withCreateProcess always calls cleanupProcess once the action is
>>> done, right?
>> I guess you would have to replace `start'` with something like:
>> ```haskell
>> withEngine :: Time unit -> ... -> (Engine -> IO ()) -> IO ()
>> ```
>> And let your user provide the `Engine -> IO ()` callback.
> Ahh, of course, push the problem up the caller chain :-)
> And I guess if that caller needs a sort of handle, it would do it with a
> channel to communicate with its action?
> I know I was asking for idiomatic code, and I am thankful for
> the pointer.  Exception safety is clearly a good goal. But, I wonder, is
> limiting the API in such a way worth the trouble?
> It looks like this change would trigger a complete rewrite of
> the current module and all its clients.
> I'd like to be confident this is the right thing to do before doing that.

Beware I'm in the same boat and I can't tell if this is the right thing
to do :-)

I like the with... idiom because it is straighforward and it should not
limit the API, users just need to provide a callback instead of binding
to get the resource.

Though it is more tedious to use from a REPL, where you need to prefix
each input with `withEngine ... $ \engine -> ...`

>> Alternatively, perhaps the `managed` library can be used to get
>> something closer to your existing `start'`, e.g.:
>> http://hackage.haskell.org/package/managed-1.0.8/docs/Control-Monad-Managed.html
> Ahh, thanks again.  I'll have to digest all of this.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 515 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20210531/d7dc0f87/attachment.sig>

More information about the Haskell-Cafe mailing list