How to retrieve Redis database values in order of key using Python

Viewed 284

I have a python application where a client retrieves csv data row by row from a server using a grpc stream. Data from each row is added to a dictionary which in turn is saved to a redis database. When I attempt to retrieve the data from the redis database in a seperate flask application, the data is not coming out in order and is duplicating much of the time. How can I retrieve the data in order of the key without duplicates?

Client

def run():

    #Average number of comments metric
    average_num_comments = 0
    response_count = 0
    comment_count = 0

    try:
        conn = redis.StrictRedis(host='redis', port=6379)
        conn.flushdb()
    except Exception as ex:
        print('Error:', ex) 
         
    while True:
        with grpc.insecure_channel('redditserver:50051') as channel:

            stub = route_guide_pb2_grpc.RouteGuideStub(channel)
            responses = stub.SendRedditPost(route_guide_pb2.PostRequestReddit(response='Recieved'))

            #Single post with most letters in title
            lg_post_title = ''

            for response in responses:

                response_count += 1
                comment_count = int(response.num_comments) + comment_count
                average_num_comments = avg(response_count, comment_count)

                if (len(response.title) > len(lg_post_title)):
                    lg_post_title = response.title

                redisdict = {"Largest Post Title": lg_post_title, "Comment Count": comment_count, "Average No. Comments": average_num_comments}

                try:
                    conn = redis.StrictRedis(host='redis', port=6379)
                    conn.hmset(response_count, redisdict)
   
                except Exception as ex:
                    print('Error:', ex)
                time.sleep(2)

Flask Application

[![@app.route('/')
def get_page():

    data = ''

    try:
        conn = redis.StrictRedis(host='redis', port=6379, decode_responses=True)
        for key in conn.scan_iter():

            value = conn.hgetall(key)

            data = value

            time.sleep(2)
            print("KEY: " + key, file=sys.stderr)
            print(data, file=sys.stderr)

    except Exception as ex:
        data = 'Error:' + str(ex)
    return render_template('index.html', x=data)][1]][1]
0 Answers
Related