0

In a flask application , I have these 2 apis exposed like this

@app.route('/someapp',methods=['POST'])
def someapp():  
    bucket_id = config.bucket_id
    folder = config.folder_name
    request_data = request.get_json()
    response_out = predstruct(bucket_id,folder,request_data) 
    return response_out

@app.route('/anotherapp',methods=['POST'])
def anotherapp():  
    bucket_id = config.bucket_id
    folder = config.folder_name
    request_data = request.get_json()
    response_out = predstruct(bucket_id,folder,request_data)   
    return response_out

def predstruct(bucket_id, folder, request_payload):
    df = pickle_read_gcs_pickle_current(bucket_id,folder)
    neigbors_dict = read_gcs_nn_current(bucket_id,folder)
    ....................

This is how I am reading the JSON file from a GCS Bucket

def read_gcs_nn_current(bucket_id,folder):

    from google.cloud import storage
    bucket = storage.Client().bucket(bucket_id)

# Setting the artifacts Prefix      
    nn_neighbour_current_blob = 'users_nn'+ '.json' 

#   Creating Blobs to store artifacts in GS-Buckets 
    blob = bucket.blob('{}/{}'.format(
        datetime.datetime.now().strftime(folder),
        nn_neighbour_current_blob))

#  Saves temporarily the pickle file 
    temp_model_location = '/tmp/' + nn_neighbour_current_blob
    blob.download_to_filename(temp_model_location)
    with open(temp_model_location, "r") as outfile:
        neigbors_dict = json.load( outfile )

    return neigbors_dict

This code runs file when the apis are called sequentially. But if I try to call the someapp and anotherapp apis parallely, sometimes I am getting an error while the json.load() . It fails with the following error:

File "/usr/local/lib/python3.7/json/decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 3544112 (char 3544111)

The structure of the file is correct as this file gets loaded when sequential calls are made to the apis

Ayan Biswas
  • 1,469
  • 8
  • 32
  • 55
  • Does this answer your question? [json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190)](https://stackoverflow.com/questions/48140858/json-decoder-jsondecodeerror-extra-data-line-2-column-1-char-190) – Alex Oct 05 '21 at 20:53

0 Answers0