0

I have this python application, which will run every 5 seconds (when executed as python3 app.py) but not expecting the same output when dockerized.

Here's my app.py

import os
import threading
from pymongo import MongoClient

# get the values of the environment variables 
MONGODB_HOSTNAME = os.environ['MONGO_URI']
MONGODB_USERNAME = os.environ['MONGODB_USERNAME']
MONGODB_PASSWORD = os.environ['MONGODB_PASSWORD']
MONGODB_DATABASE = os.environ['MONGODB_LOGS_DATABASE']
MONGODB_COLLECTION_NAME = os.environ['MONGODB_COLLECTION_NAME']

client = MongoClient("mongodb://"+MONGODB_USERNAME+":"+MONGODB_PASSWORD+"@mongodb/admin")

def get_count():

    threading.Timer(5.0, get_count).start()
    records = get_records()
    print("Getting count...")

    # Check if last 10 records have 10 failed attempts
    response_codes = list(map(lambda x : x['ResponseCode'], records))
    
    codes = []

    count = 0
    for response in response_codes:
        codes.append(response)
        if (response == '200'):
            count = count+1
    
    return str(count)

def get_records():
    db = client[MONGODB_DATABASE]

    collection = db[MONGODB_COLLECTION_NAME]
    cursor = collection.find()

    item = {}
    data = []

    if cursor:
        for record in cursor:
            item = {'ResponseCode' : record['ResponseCode']}
            data.append(item)

    return data


get_count()

When I try it, the output is as below:

Getting count...
<after 5 seconds>
Getting count...
<after 5 seconds>
Getting count...
<after 5 seconds>
.
.
.

And here's the Dockerfile for the above app

FROM python:3.6.8-alpine3.9

LABEL MAINTAINER="Jananath Banuka [email protected]"

ENV GROUP_ID=1000 \
    USER_ID=1000

WORKDIR /tmp/

ADD . /tmp/
RUN pip install -r requirements.txt

CMD ["python3", "app.py"]

When when I run and build and see the logs, I can only see the below output and nothing after that:

Getting count...

Can someone help me why it is only printing it once but not recurring?

PS: environment variables are passed in a docker-compose.yml

Thank you!

2

1 Answer 1

2

Set environment variable below in order to force the stdout and stderr streams to be unbuffered

ENV PYTHONUNBUFFERED=1
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.