Skip to content

Commit

Permalink
Using pythonunbuffered instead of flushing the sys.stdout (#3937)
Browse files Browse the repository at this point in the history
* Using pythonunbuffered instead of flushing the sys.stdout
  • Loading branch information
dinagraves authored Jun 2, 2020
1 parent fcf50b1 commit e0cfe08
Show file tree
Hide file tree
Showing 12 changed files with 21 additions and 22 deletions.
3 changes: 3 additions & 0 deletions run/hello-broken/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
# https://hub.docker.com/_/python
FROM python:3.8

# Allow statements and log messages to immediately appear in the Cloud Run logs
ENV PYTHONUNBUFFERED True

# Copy application dependency manifests to the container image.
# Copying this separately prevents re-running pip install on every code change.
COPY requirements.txt ./
Expand Down
7 changes: 0 additions & 7 deletions run/hello-broken/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# [START run_broken_service]
import json
import os
import sys

from flask import Flask

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

# Flush the stdout to avoid log buffering.
sys.stdout.flush()

return f"Hello {NAME}"
# [END run_broken_service]

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

# Flush the stdout to avoid log buffering.
sys.stdout.flush()

return f"Hello {NAME}"


Expand Down
3 changes: 3 additions & 0 deletions run/image-processing/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
# https://hub.docker.com/_/python
FROM python:3.8

# Allow statements and log messages to immediately appear in the Cloud Run logs
ENV PYTHONUNBUFFERED True

# Copy application dependency manifests to the container image.
# Copying this separately prevents re-running pip install on every code change.
COPY requirements.txt ./
Expand Down
3 changes: 0 additions & 3 deletions run/image-processing/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import base64
import json
import os
import sys

from flask import Flask, request

Expand Down Expand Up @@ -62,8 +61,6 @@ def index():

try:
image.blur_offensive_images(data)
# Flush the stdout to avoid log buffering.
sys.stdout.flush()
return ('', 204)

except Exception as e:
Expand Down
3 changes: 3 additions & 0 deletions run/logging-manual/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
# https://hub.docker.com/_/python
FROM python:3.8

# Allow statements and log messages to immediately appear in the Cloud Run logs
ENV PYTHONUNBUFFERED True

# Copy application dependency manifests to the container image.
# Copying this separately prevents re-running pip install on every code change.
COPY requirements.txt ./
Expand Down
4 changes: 0 additions & 4 deletions run/logging-manual/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import json
import os
import sys

from flask import Flask, request

Expand Down Expand Up @@ -52,9 +51,6 @@ def index():
print(json.dumps(entry))
# [END run_manual_logging]

# Flush the stdout to avoid log buffering.
sys.stdout.flush()

return 'Hello Logger!'


Expand Down
3 changes: 3 additions & 0 deletions run/markdown-preview/editor/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
# https://hub.docker.com/_/python
FROM python:3.8-slim

# Allow statements and log messages to immediately appear in the Cloud Run logs
ENV PYTHONUNBUFFERED True

# Copy application dependency manifests to the container image.
# Copying this separately prevents re-running pip install on every code change.
COPY requirements.txt ./
Expand Down
3 changes: 3 additions & 0 deletions run/markdown-preview/renderer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
# https://hub.docker.com/_/python
FROM python:3.8-slim

# Allow statements and log messages to immediately appear in the Cloud Run logs
ENV PYTHONUNBUFFERED True

# Copy application dependency manifests to the container image.
# Copying this separately prevents re-running pip install on every code change.
COPY requirements.txt ./
Expand Down
3 changes: 3 additions & 0 deletions run/pubsub/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
# https://hub.docker.com/_/python
FROM python:3.8

# Allow statements and log messages to immediately appear in the Cloud Run logs
ENV PYTHONUNBUFFERED True

# Copy application dependency manifests to the container image.
# Copying this separately prevents re-running pip install on every code change.
COPY requirements.txt ./
Expand Down
4 changes: 0 additions & 4 deletions run/pubsub/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# [START run_pubsub_server_setup]
import base64
import os
import sys

from flask import Flask, request

Expand Down Expand Up @@ -46,9 +45,6 @@ def index():

print(f'Hello {name}!')

# Flush the stdout to avoid log buffering.
sys.stdout.flush()

return ('', 204)
# [END run_pubsub_handler]

Expand Down
3 changes: 3 additions & 0 deletions run/system-package/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
# https://hub.docker.com/_/python
FROM python:3.8

# Allow statements and log messages to immediately appear in the Cloud Run logs
ENV PYTHONUNBUFFERED True

# [START run_system_package_ubuntu]
RUN apt-get update -y && apt-get install -y \
graphviz \
Expand Down
4 changes: 0 additions & 4 deletions run/system-package/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import os
import subprocess
import sys

from flask import Flask, make_response, request

Expand All @@ -36,9 +35,6 @@ def index():
except Exception as e:
print("error: {}".format(e))

# Flush the stdout to avoid log buffering.
sys.stdout.flush()

# If no graphviz definition or bad graphviz def, return 400
if "syntax" in str(e):
return "Bad Request: {}".format(e), 400
Expand Down

0 comments on commit e0cfe08

Please sign in to comment.