Friday, January 25, 2008

Improvements to io.launcher

Factor's integration with the host operating system just keeps on getting better. I already blogged about Factor's awesome process launcher library and today it got even better.

First of all, run-process and run-detached return an instance of process. These objects can be passed to wait-for-process which waits for the process to exit and returns the exit code.

Unlike before, waiting for a process to exit does not block the entire Factor VM, instead process exit notifications are hooked into the I/O event queue provided by our Windows and Unix "native I/O" support.

Second, while we already have flexible input/output redirection in the form of <process-stream>, redirecting input or output to a file was a bit bothersome; you had to start a thread which would read from the file and write to the pipe, or vice versa. Now, there's a new feature for redirecting input and output directly to files:
H{
{ +command+ "ls /etc" }
{ +stdout+ "listing.txt" }
} run-process

Similarly, you can provide +stderr+ or +stdin+ keys. The value +closed+ may also be given.

Of course, if you have a program which needs to munge the output of a process in some way, or send it commands generated on the fly, you can still use a pipe, but for the common use-case of redirecting to files, having direct support is not only easier on the programmer but more efficient.

Finally, you can mix the above redirection features with <process-stream>. For example,
H{
{ +command+ "sort" }
{ +stdin+ "unsorted-data.txt" }
} <process-stream> lines

This starts the Unix "sort" command; it will take standard input from unsorted-data.txt, and standard output will be sent to your Factor process via a pipe. The lines word reads lines of text from the pipe until EOF.

As usual, both new features work and have been tested on both Unix and Windows. To me, it is important that Factor is not a second-class citizen on Windows.

Cool stuff!

3 comments:

Falicon said...

Awesome stuff! I agree that it's important to keep things up to speed on Windows systems...I know a lot of developers love/prefer Unix and Linux, and lots have jumped into the Apple world as well...but most (U.S.) homes still have more Windows PCs than anything else...if you want a language like Factor to gain traction, playing nice in an area that has the widest reach is the way to go IMHO :-D

Slava Pestov said...

Falicon: my viewpoint is that Mac, Windows and Unix all suck equally at doing different things.

My laptop runs Windows, my desktop runs Mac OS X, and my servers run Linux, and all of my machines are a pain to use. ;-)

Yoshinori Tahara said...

I'd like to get an exit code of a process. How do I do this?