From 08b2ff3ae5d0c05917de73a19e24c4e1b2655d94 Mon Sep 17 00:00:00 2001 From: lg Date: Thu, 4 Jan 2018 23:17:42 +0800 Subject: [PATCH] add setlinebuf helper function and call it in tutorial_mnist_distributed.py --- example/tutorial_mnist_distributed.py | 2 ++ tensorlayer/distributed.py | 4 ---- tensorlayer/ops.py | 13 +++++++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/example/tutorial_mnist_distributed.py b/example/tutorial_mnist_distributed.py index 43c81a3fa..294a4b242 100644 --- a/example/tutorial_mnist_distributed.py +++ b/example/tutorial_mnist_distributed.py @@ -14,6 +14,8 @@ import tensorflow as tf import tensorlayer as tl +# set buffer mode to _IOLBF for stdout +tl.ops.setlinebuf() # load environment for distributed training task_spec = tl.distributed.TaskSpec() diff --git a/tensorlayer/distributed.py b/tensorlayer/distributed.py index d89bc8e80..6e9c223ad 100644 --- a/tensorlayer/distributed.py +++ b/tensorlayer/distributed.py @@ -7,10 +7,6 @@ import json import time -# Disable buffer for stdout. -# When running in container, or other environments where stdout is redirected, -# the default buffer behavior will seriously delay the message written by `print`. -# sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0) class TaskSpecDef(object): """Specification for the distributed task with the job name, index of the task, diff --git a/tensorlayer/ops.py b/tensorlayer/ops.py index a2972e0f4..012d1b68f 100644 --- a/tensorlayer/ops.py +++ b/tensorlayer/ops.py @@ -134,7 +134,20 @@ def set_gpu_fraction(sess=None, gpu_fraction=0.3): return sess +def setlinebuf(): + """Set buffer mode to _IOLBF for stdout. + When running in container, or other environments where stdout is redirected, + the default buffer behavior will seriously delay the message written by `print`. + TODO: this method should be called automatically by default. + + References + ----------- + - ``_ + - ``_ + - `man setlinebuf `_ + """ + sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1) def disable_print():