开发者

Can I use a smaller buffer for NSFileHandleReadCompletionNotification?

I'm reading NSFileHandleReadCompletionNotification messages from the NSNotificationCenter to receive messages from an NSTask. The problem is that the command line program I'm calling is relatively slow to output lines, and it seems that the NSFileHandleReadCompletionNotification message gets posted relatively infrequently (I guess when the buffer fills up).

Is there another Notification message I can use that would post a notification or every line, or is there a way to开发者_如何学C make the buffer smaller?

Edit: To be clear, I read that the buffer size is "imited to the buffer size of the underlying operating system" in the NSFileHandle documentation, so, I'm hoping there's some other trick.


If you read from NSFileHandle by

- (void)readInBackgroundAndNotify

method (right?), and parse data in the NSFileHandleReadCompletionNotification handler - so, the buffer size isn't limited - all "availableData" will be read in the background before you receive the notification, then you call the readInBackgroundAndNotify again to fetch next portion...

However I believe your issue happens due to well known I/O buffering technology.

You should turn buffering Off at the task-command side. Example, if you call a Perl script, just put the line

$|=1;

or

use IO::Handle;
STDOUT->autoflush(1);
STDERR->autoflush(1);

near to the top of the script.

For a C-program - use setvbuf function to set buffer size to zero.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜