Skip to content

Commit 7f0ccb8

Browse files
authored
Merge pull request #268 from lgarithm/setlinebuf
add setlinebuf helper function and call it in distributed example
2 parents 3beebb9 + 08b2ff3 commit 7f0ccb8

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

example/tutorial_mnist_distributed.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import tensorflow as tf
1515
import tensorlayer as tl
1616

17+
# set buffer mode to _IOLBF for stdout
18+
tl.ops.setlinebuf()
1719

1820
# load environment for distributed training
1921
task_spec = tl.distributed.TaskSpec()

tensorlayer/distributed.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
import json
88
import time
99

10-
# Disable buffer for stdout.
11-
# When running in container, or other environments where stdout is redirected,
12-
# the default buffer behavior will seriously delay the message written by `print`.
13-
# sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0)
1410

1511
class TaskSpecDef(object):
1612
"""Specification for the distributed task with the job name, index of the task,

tensorlayer/ops.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,20 @@ def set_gpu_fraction(sess=None, gpu_fraction=0.3):
134134
return sess
135135

136136

137+
def setlinebuf():
138+
"""Set buffer mode to _IOLBF for stdout.
139+
When running in container, or other environments where stdout is redirected,
140+
the default buffer behavior will seriously delay the message written by `print`.
137141
142+
TODO: this method should be called automatically by default.
143+
144+
References
145+
-----------
146+
- `<https://docs.python.org/2/library/functions.html#open>`_
147+
- `<https://docs.python.org/3/library/functions.html#open>`_
148+
- `man setlinebuf <https://linux.die.net/man/3/setlinebuf>`_
149+
"""
150+
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1)
138151

139152

140153
def disable_print():

0 commit comments

Comments
 (0)