0

I'm working on a macOS application that uses NSTask to launch a Python script to perform an operation. The Python implementation displays a progress bar while the operation is running.

The Python script is using the Click package: Click - Showing Progress Bars

NSTask is not reporting changes to this progress bar through the usual output readers. It is correctly notifying me of output after the progress bar has reached 100% and a final line has been written. I assume there's some sort of flushing that is not happening to inform NSTask that data is available.

NSTask *task = [[NSTask alloc] init];
task.arguments = ...;
task.environment = ...;
task.executableURL = ...;

NSPipe *pipe = [[NSPipe alloc] init];
task.standardOutput = pipe;

NSFileHandle *outputHandle = pipe.fileHandleForReading;

outputHandle.readabilityHandler = ^(NSFileHandle *handle) {
  NSString *output = [[NSString alloc] initWithData:handle.availableData encoding:NSUTF8StringEncoding];
  NSLog(@"%@", output);
};

[task launch];
[task waitUntilExit];

NSLog(@"Task should have completed...");

Terminal Output:

  [#####     ]  50%
Command Finished.

NSTask Output:

Command Finished.
Task should have completed...

While running the command from a Terminal window, the progress bar will update and then finish with the last line. When running from an NSTask the only output I receive is the last line. No updates are received for the progress bar. Is there a way to receive those progress bar updates?

Same question from 12 years ago: read ASCII progress bar of NSStask

kennyc
  • 5,490
  • 5
  • 34
  • 57
  • Does this answer your question? [How to use a determinate NSProgressIndicator to check on the progress of NSTask? - Cocoa](https://stackoverflow.com/questions/8889006/how-to-use-a-determinate-nsprogressindicator-to-check-on-the-progress-of-nstask) – Willeke Feb 03 '22 at 09:24
  • Is it missing `[outpuHandle waitForDataInBackgroundAndNotify]`? – Larme Feb 03 '22 at 09:44
  • @Willeke Not really. I'm not using an NSProgressIndicator. The progress in question is being produced by the Python script and output to the Terminal as ASCII art. I just want to be able to read that output in the same way I can read normal output from a task. – kennyc Feb 03 '22 at 10:39
  • @Larme Don't think that applies if you're using the newer, `readabilityHandler` API. That method is for those waiting to receive an `NSNotification` of data being available. – kennyc Feb 03 '22 at 10:42
  • The answer in the linked question doesn't call `waitUntilExit` and It observes notifications. Have you tried this? – Willeke Feb 03 '22 at 12:18
  • @Willeke I have tried that and it's unrelated to the issue at hand. The problem I'm experiencing is that the `readabilityHandler` is not getting called when the Python script updates its ASCII progress bar. I *assume* this is because the Terminal is not getting flushed in a way that `NSTask` understands. (The ASCII progress bar is a single line that overwrites itself.) What I'm wondering is if there's a way for `NSTask` to still see this output even though it might not end in a newline. Once the operation has finished, a new line is emitted and the handler is called as expected. – kennyc Feb 03 '22 at 12:56
  • I've done something similar in the past. How can we reproduce the issue? I'm not familiar with Python. – Willeke Feb 03 '22 at 13:17

0 Answers0