[squeak-dev] OSProcess streaming and early termination

Bert Freudenberg bert at freudenbergs.de
Fri Dec 7 15:54:31 UTC 2012


Hi David, folks,

I need to read output from an external command which is potentially too large to fit in memory. So I want to read from the pipe, and possibly have to terminate early.

Here is what I have so far - "od" is an example only of course, but I need to be able to use arg arrays and a working dir and it illustrates the problem:

| process1 process2 |
process1 := PipeableOSProcess new: '/usr/bin/od'
	arguments: {'-v'. '-t'. 'x1'. (Smalltalk imageName copyAfterLast: $/) asVmPathName}
	environment: nil descriptors: nil
	workingDir: Smalltalk imagePath asVmPathName
	errorPipelineStream: nil.
process2 := ExpressionEvaluator block: [:stdin | stdin next: 1000].
process2 pipeToInput: process1 pipeFromOutput.
process1 value.
process2 value.
process2 succeeded
	ifFalse: [process2 errorUpToEnd]
	ifTrue: [process2 output]

This does get me the first 1000 bytes od the "od" output. 

However, this seems like more hoops than necessary to jump through - I have to set up the processes first, then pipe them, then execute them, only then can I access the output. Finding the right sequence required reading a lot of code and guessing. Is there a more convenient way? I tried "|" but it only wants a string argument, not an ExpressionEvaluator object.

Secondly, even though the external process should be gone after reading 1000 chars, it appears that it is still running. Do I manually have to kill it? I tried #closePipes but that does an upToEnd which in this case is counterproductive because it churns through a Gigabyte of data.

Thirdly, how do I find out about errors in the external process? E.g. if I misspell the command there is nothing in its stderr, it all seems to fail silently.

Or maybe I'm going about this in a completely wrong way? I could not find an example anywhere in OSProcess that would pipe command output into Smalltalk code.

Help appreciated :)

- Bert -




More information about the Squeak-dev mailing list