Skip to content

Commit fe0a07f

Browse files
committed
%px: show outputs on error
always show outputs on completion, on success or failure - simplify conditions a bit to reduce repetition - display_outputs shows captured outputs, even on failure
1 parent 5c77a1b commit fe0a07f

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

ipyparallel/client/asyncresult.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ def display_outputs(self, groupby="type", result_only=False):
990990
stderrs = self.stderr
991991
execute_results = self.execute_result
992992
output_lists = self.outputs
993-
results = self.get()
993+
results = self.get(return_exceptions=True)
994994

995995
targets = self.engine_id
996996

ipyparallel/client/magics.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@ def result(self, line=''):
330330
if self.last_result is None:
331331
raise UsageError(NO_LAST_RESULT)
332332

333+
if args.save_name:
334+
self.shell.user_ns[args.save_name] = self.last_result
335+
return
336+
333337
self.last_result.get()
334338
self.last_result.display_outputs(groupby=args.groupby)
335339

@@ -402,37 +406,26 @@ def parallel_execute(
402406
# wait for 'quick' results before showing progress
403407
tic = time.perf_counter()
404408
deadline = tic + progress_after
405-
try:
406-
result.get(timeout=progress_after)
407-
remaining = max(deadline - time.perf_counter(), 0)
408-
result.wait_for_output(timeout=remaining)
409-
except TimeoutError:
410-
pass
411-
except error.CompositeError as e:
412-
if stream_output:
413-
# already streamed, show an abbreviated result
414-
raise error.AlreadyDisplayedError(e) from None
415-
else:
416-
raise
417-
else:
418-
finished_waiting = True
409+
result.wait(timeout=progress_after)
410+
remaining = max(deadline - time.perf_counter(), 0)
411+
result.wait_for_output(timeout=remaining)
412+
finished_waiting = result.done()
419413

420414
if not finished_waiting:
421415
if progress_after >= 0:
422416
# not an immediate result, start interactive progress
423417
result.wait_interactive()
424-
result.wait_for_output()
425-
try:
426-
result.get()
427-
except error.CompositeError as e:
428-
if stream_output:
429-
# already streamed, show an abbreviated result
430-
raise error.AlreadyDisplayedError(e) from None
431-
else:
432-
raise
418+
result.wait_for_output(1)
419+
420+
try:
421+
result.get()
422+
except error.CompositeError as e:
423+
if stream_output:
424+
# already streamed, show an abbreviated result
425+
raise error.AlreadyDisplayedError(e) from None
426+
else:
427+
raise
433428
# Skip redisplay if streaming output
434-
if not stream_output:
435-
result.display_outputs(groupby)
436429
except KeyboardInterrupt:
437430
if signal_on_interrupt is not None:
438431
print(
@@ -444,6 +437,14 @@ def parallel_execute(
444437
)
445438
else:
446439
raise
440+
finally:
441+
# always redisplay outputs if not streaming,
442+
# on both success and error
443+
444+
if not stream_output:
445+
# wait for at most 1 second for output to be complete
446+
result.wait_for_output(1)
447+
result.display_outputs(groupby)
447448
else:
448449
# return AsyncResult only on non-blocking submission
449450
return result

ipyparallel/tests/test_asyncresult.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,20 @@ def test_display_empty_streams_engine(self):
288288
self.assertEqual(io.stderr, '')
289289
self.assertEqual(io.stdout, '')
290290

291+
def test_display_output_error(self):
292+
"""display_outputs shows output on error"""
293+
self.minimum_engines(1)
294+
295+
v = self.client[-1]
296+
ar = v.execute("print (5555)\n1/0")
297+
ar.get(5, return_exceptions=True)
298+
ar.wait_for_output(5)
299+
with capture_output() as io:
300+
ar.display_outputs()
301+
self.assertEqual(io.stderr, '')
302+
self.assertEqual('5555\n', io.stdout)
303+
assert 'ZeroDivisionError' not in io.stdout
304+
291305
def test_await_data(self):
292306
"""asking for ar.data flushes outputs"""
293307
self.minimum_engines(1)

0 commit comments

Comments
 (0)