title: NSTask's real-time output
body: <p>I have a PHP script which has mutliple <code>sleep()</code> commands. I would like to execute it in my application with <code>NSTask</code>. My script looks like this:</p>
  
  <pre><code>echo "first\n"; sleep(1); echo "second\n"; sleep(1); echo "third\n";
  </code></pre>
  
  <p>I can execute my task asynchronously using notifications:</p>
  
  <pre><code>- (void)awakeFromNib {
      NSTask *task = [[NSTask alloc] init];
      [task setLaunchPath: @"/usr/bin/php"];
  
      NSArray *arguments;
      arguments = [NSArray arrayWithObjects: @"-r", @"echo \"first\n\"; sleep(1); echo \"second\n\"; sleep(1); echo \"third\n\";", nil];
      [task setArguments: arguments];
  
      NSPipe *p = [NSPipe pipe];
      [task setStandardOutput:p];
  
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskExited:) name:NSTaskDidTerminateNotification object:task];
  
      [task launch];
  
  }
  
  - (void)taskExited:(NSNotification *)notif {
      NSTask *task = [notif object];
      NSData *data = [[[task standardOutput] fileHandleForReading] readDataToEndOfFile];
      NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
      NSLog(@"%@",str);
  }
  </code></pre>
  
  <p>My output is (after 2 seconds, of course):</p>
  
  <pre><code>2011-08-03 20:45:19.474 MyApp[3737:903] first
  second
  third
  </code></pre>
  
  <hr>
  
  <p><strong>My question is: how can I get theese three words immediately after they are printed?</strong></p>