Skip to content

Commit 87fb070

Browse files
committed
By default we use PIPE connections to capture stdout/stderr that are then explicitly printed if logfile and errfile are not already provided. Also, since we want to capture stdout/stderr as text, we need to specify text=True for that to happen. We also log to the root logger so that forward_logging() (hopefully) works.
1 parent 340946b commit 87fb070

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

ipsframework/services.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,13 @@ def launch(executable: Any,
9494
import logging
9595
from dask.distributed import get_worker # pylint: disable=import-outside-toplevel
9696

97-
# Later, we use Client.forward_logging() to handle these log messages.
98-
log = logging.getLogger('launch')
99-
10097
worker = get_worker()
10198
task_key = worker.get_current_task()
10299

100+
# Later, we use Client.forward_logging() to handle these log messages. We
101+
# access the root logger for forward_logging() to work.
102+
log = logging.getLogger()
103+
103104
log.info(f'Launching task {task_name} with id {task_key!s} and '
104105
f'worker {worker.name!s} in {working_dir}')
105106
print(f'Launching task {task_name} with id {task_key!s} and '
@@ -114,7 +115,7 @@ def launch(executable: Any,
114115
# via a subprocess.Popen()
115116

116117
# Do we write the Popen stdout to sys.stdout or to a file?
117-
subprocess_stdout = sys.stdout
118+
subprocess_stdout = subprocess.PIPE
118119
close_stdout = False # is true if we need to later close the file
119120
try:
120121
log_filename = kwargs['logfile']
@@ -131,7 +132,7 @@ def launch(executable: Any,
131132
print(f'Task output log file: {log_path}')
132133

133134
# Repeat the same for stderr
134-
subprocess_errfile = subprocess.STDOUT
135+
subprocess_errfile = subprocess.PIPE
135136
close_stderr = False
136137
try:
137138
subprocess_errfile = kwargs['errfile']
@@ -224,6 +225,7 @@ def launch(executable: Any,
224225
stdout=subprocess_stdout,
225226
stderr=subprocess_errfile,
226227
cwd=working_dir_path,
228+
text=True,
227229
preexec_fn=os.setsid, env=new_env) # noqa: PLW1509 (TODO: look into this to potentially avoid deadlocks)
228230
except Exception as e:
229231
worker.log_event('ips',
@@ -287,11 +289,17 @@ def launch(executable: Any,
287289
log.error(f'Task {task_name} with command {cmd} failed with {e!s}')
288290
print(f'Task {task_name} with command {cmd} failed with {e!s}')
289291
finally:
292+
if 'logfile' is not in kwargs:
293+
print(process.stdout.read() if process and process.stdout else '')
294+
if 'errfile' is not in kwargs:
295+
print(process.stderr.read() if process and process.stderr else '')
296+
290297
if close_stdout:
291298
subprocess_stdout.close()
292299

293300
if close_stderr:
294301
subprocess_errfile.close()
302+
295303
elif isinstance(executable, Callable):
296304
# binary not a string, but is a python callable, so we call it directly
297305
# with the given *args

0 commit comments

Comments
 (0)