Skip to content

Commit e0cfe08

Browse files
authored
Using pythonunbuffered instead of flushing the sys.stdout (GoogleCloudPlatform#3937)
* Using pythonunbuffered instead of flushing the sys.stdout
1 parent fcf50b1 commit e0cfe08

File tree

12 files changed

+21
-22
lines changed

12 files changed

+21
-22
lines changed

run/hello-broken/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
# https://hub.docker.com/_/python
1919
FROM python:3.8
2020

21+
# Allow statements and log messages to immediately appear in the Cloud Run logs
22+
ENV PYTHONUNBUFFERED True
23+
2124
# Copy application dependency manifests to the container image.
2225
# Copying this separately prevents re-running pip install on every code change.
2326
COPY requirements.txt ./

run/hello-broken/main.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# [START run_broken_service]
1616
import json
1717
import os
18-
import sys
1918

2019
from flask import Flask
2120

@@ -35,9 +34,6 @@ def index():
3534
raise Exception("Missing required service parameter.")
3635
# [END run_broken_service_problem]
3736

38-
# Flush the stdout to avoid log buffering.
39-
sys.stdout.flush()
40-
4137
return f"Hello {NAME}"
4238
# [END run_broken_service]
4339

@@ -58,9 +54,6 @@ def improved():
5854
print(json.dumps(error_message))
5955
# [END run_broken_service_upgrade]
6056

61-
# Flush the stdout to avoid log buffering.
62-
sys.stdout.flush()
63-
6457
return f"Hello {NAME}"
6558

6659

run/image-processing/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
# https://hub.docker.com/_/python
1717
FROM python:3.8
1818

19+
# Allow statements and log messages to immediately appear in the Cloud Run logs
20+
ENV PYTHONUNBUFFERED True
21+
1922
# Copy application dependency manifests to the container image.
2023
# Copying this separately prevents re-running pip install on every code change.
2124
COPY requirements.txt ./

run/image-processing/main.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import base64
1717
import json
1818
import os
19-
import sys
2019

2120
from flask import Flask, request
2221

@@ -62,8 +61,6 @@ def index():
6261

6362
try:
6463
image.blur_offensive_images(data)
65-
# Flush the stdout to avoid log buffering.
66-
sys.stdout.flush()
6764
return ('', 204)
6865

6966
except Exception as e:

run/logging-manual/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
# https://hub.docker.com/_/python
1717
FROM python:3.8
1818

19+
# Allow statements and log messages to immediately appear in the Cloud Run logs
20+
ENV PYTHONUNBUFFERED True
21+
1922
# Copy application dependency manifests to the container image.
2023
# Copying this separately prevents re-running pip install on every code change.
2124
COPY requirements.txt ./

run/logging-manual/main.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import json
1616
import os
17-
import sys
1817

1918
from flask import Flask, request
2019

@@ -52,9 +51,6 @@ def index():
5251
print(json.dumps(entry))
5352
# [END run_manual_logging]
5453

55-
# Flush the stdout to avoid log buffering.
56-
sys.stdout.flush()
57-
5854
return 'Hello Logger!'
5955

6056

run/markdown-preview/editor/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
# https://hub.docker.com/_/python
1717
FROM python:3.8-slim
1818

19+
# Allow statements and log messages to immediately appear in the Cloud Run logs
20+
ENV PYTHONUNBUFFERED True
21+
1922
# Copy application dependency manifests to the container image.
2023
# Copying this separately prevents re-running pip install on every code change.
2124
COPY requirements.txt ./

run/markdown-preview/renderer/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
# https://hub.docker.com/_/python
1717
FROM python:3.8-slim
1818

19+
# Allow statements and log messages to immediately appear in the Cloud Run logs
20+
ENV PYTHONUNBUFFERED True
21+
1922
# Copy application dependency manifests to the container image.
2023
# Copying this separately prevents re-running pip install on every code change.
2124
COPY requirements.txt ./

run/pubsub/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
# https://hub.docker.com/_/python
1919
FROM python:3.8
2020

21+
# Allow statements and log messages to immediately appear in the Cloud Run logs
22+
ENV PYTHONUNBUFFERED True
23+
2124
# Copy application dependency manifests to the container image.
2225
# Copying this separately prevents re-running pip install on every code change.
2326
COPY requirements.txt ./

run/pubsub/main.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# [START run_pubsub_server_setup]
1616
import base64
1717
import os
18-
import sys
1918

2019
from flask import Flask, request
2120

@@ -46,9 +45,6 @@ def index():
4645

4746
print(f'Hello {name}!')
4847

49-
# Flush the stdout to avoid log buffering.
50-
sys.stdout.flush()
51-
5248
return ('', 204)
5349
# [END run_pubsub_handler]
5450

0 commit comments

Comments
 (0)