On Mon, Aug 13, 2012, at 04:38 PM, Ken Thomases wrote:
> On Aug 13, 2012, at 5:42 PM, Kyle Sluder wrote:
> 
> > On Mon, Aug 13, 2012, at 02:17 PM, Charlie Dickman wrote:
> >> I'm trying to get the output from a vm_stat command using the following
> >> code. My app hangs in the [vmRead availableData] call as it should if
> >> there is no data available but it never comes back. What am I doing
> >> wrong? I have searched through the sample code on apples developer site
> >> with no luck.
> >> 
> >>    NSPipe *vmPipe = [NSPipe pipe];
> >>    NSFileHandle *vmRead = [vmPipe fileHandleForReading];
> >> 
> >>    [vm setLaunchPath: @"/usr/bin/vm_stat"];        //      1 page = 4096 
> >> bytes
> >>    [vm setStandardOutput: vmPipe];
> >>    [vm launch];
> >>    NSData *vmData = nil;
> >>    int vmDataLength = 0;
> >>    do {
> >>            vmData = [vmRead availableData];
> >>            vmDataLength = [vmData length];
> >>    } while (vmDataLength != 0);
> > 
> > 
> > You can't do this. You need to run the runloop.
> 
> For what?  Neither NSPipe nor NSFileHandle rely on the runloop except for
> the InBackgroundAndNotify methods, as far as I'm aware.  They are just
> thin wrappers around system calls.

Hmm. I thought we'd established a dependency on the runloop for even
basic uses of these classes. But it appears I'm mistaken.

> 
> You need to run the runloop to get notified of an NSTask's termination,
> too, but he's not relying on that.

This is the only context in which I see NSPipe being used frequently, so
I probably just extended the runloop requirement to that case.

> 
> Of course, he is potentially blocking his main thread, if this is run on
> the main thread, which is always bad.  But the code should work.

Yeah, regardless of whether it's mandatory, it's almost certainly a good
idea. :)

--Kyle Sluder
_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to