Efficiently receiving data from an NSTask
Efficiently receiving data from an NSTask
- Subject: Efficiently receiving data from an NSTask
- From: "Martin Hairer" <email@hidden>
- Date: Sun, 6 Jul 2008 15:46:47 +0100
Hi, I am in the situation where I want to launch a helper tool,
retrieve the data that it dumps to stdout, and monitor the output of
stderr for error messages and progress control. I did figure out that
the "standard" way of doing this is along the lines of the "Moriarity"
sample code. The problem is that this is way too slow if lots of data
gets returned (typically in the region of 100MB).
Right now, I am doing the following instead. In the thread that
launched the NSTask (call that the master thread), I poll data from
stdout until the task has died:
while ([myTask isRunning]) {
NSData* data = [output availableData];
if ([datalength])
Do something with the data
}
Simultaneously, I have a separate thread in which I poll stderr until
no more. I am trying to be careful in the following way:
- The separate thread does not call [myTask isRunning], but instead
checks on a flag set by the master thread.
- After the task has died, the master thread makes sure that the
secondary thread has finished before starting cleaning up.
- The NSFileHandle for the NSPipe attahced to stderr is only ever
accessed from the secondary thread and not by the master thread.
This works like a treat and is faster by a factor 3 or so than using
the "Moriarity" implementation. However, it leaves me a bit concerned
about various warnings all over the place concerning the thread
(un)safety of NSTask and NSFileHandle. So my question is: is the kind
of approach that I am taking doable / reasonable? If not, is there an
alternative way of doing this which is more efficient than the
"Moriarty" code? Thanks a lot in advance for any help / hint,
Martin
--
HairerSoft
http://www.hairersoft.com/
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden