SWI-Prolog / packages-clib

Assorted external libraries: processes, sockets, MIME, CGI, etc.
8 stars 19 forks source link

ADDED: extra_streams() option to process_create/3 #36

Open dmchurch opened 3 years ago

dmchurch commented 3 years ago

This enables the Unix-style pattern of passing additional file descriptors to child processes beyond stdin, stdout, and stderr, so that parent and child can communicate on multiple streams. For example, a Prolog program could launch a fully-interactive child process, passing stdin/stdout/stderr connected to the TTY, while reserving a separate pair of descriptors for programmatic communication with the child.

It also fixes a couple Cmake tooling errors, which prevented Prolog from ever recognizing the availability of posix_spawn() and pipe2().

JanWielemaker commented 3 years ago

Thanks. Looks useful. I'm not too happy about adding features we cannot support cross-platform (Windows). There are surely some nice use cases for this. Somehow the changes blocked passing the environment though. Try e.g. ?- help(process_create). or process_create(path(printenv), [], [])..

Pushed an additional patch for the PlDoc comments. Code should use `text` if it is Prolog text and using double backquotes for non-Prolog text. Expressions as functor(...) do not need quotes: if they are valid Prolog text they are considered code. The old stuff is still supported. Currently PlDoc accepts mostly current markdown syntax and eventually I'd like to get rid of all the old stuff.

dmchurch commented 3 years ago

I'm not too happy about adding features we cannot support cross-platform (Windows)

I'm not thrilled about it either. Weirdly, though, it's not a technical limitation but a procedural one. You can absolutely open a stream handle and let the child process inherit it, but there's no way to assign numeric values or ordering to them, which defeats the purpose of passing the descriptors in the first place. If you know of or come up with a good way to meaningfully pass handles from parent to child, I'd be delighted to implement it!

dmchurch commented 3 years ago

Somehow the changes blocked passing the environment though.

This looks to be an existing issue with the spawn method, which wasn't getting used before because of the configuration bug. I'll look into fixing it; until then work around with process_set_method(fork) or process_set_method(vfork).