Home > Blockchain >  Inconsistent performance of a Python Flask app using Threading on Google Cloud Run
Inconsistent performance of a Python Flask app using Threading on Google Cloud Run

Time:02-07

I'm trying to understand seemingly inconsistent performance of a Python Flask app on Google Cloud Run. The app takes an image file as input and does some pre-processing using cv2 and then OCR using Tesseract. I use Python threading to parallelize the image and OCR processing but even so the total processing can take 10 to 20 sec.

@app.route('/process_image/<path:image_path>')
def process_image(image_path):
    processing_results = process_input_image(image_path)

    return processing_results

def process_input_image(image_path)
    # process_input_image uses Python Threads to parallelize image processing
    for work in list_of_image_processing_work:
        th = Thread(target=do_image_work, args=(work, results))
        th.start()
    # wait for all threads to complete
    return results

I wanted to return a more immediate response indicating the processing would take a bit more time. So, I tried the following change to start a thread on the image processing call. Net, I'm starting a thread on a call to a routine that itself starts a series of threads to do image processing.

@app.route('/process_image/<path:image_path>')
def process_image(image_path):
    th = Thread(target=process_input_image, args=(image_path, results))
    th.start()

    return 'This\'ll just be a moment or so.'

But, as soon as I added this second layer of threading, processing on Google Cloud Run seemed to stall. Looking at the logs I can see routine process_input_image start, but progress seems to inch along, almost like it's a low priority, and ultimately never completes. I'm wondering if this is a thread priority thing.

CodePudding user response:

When the HTTP Request returns the message, the CPU is idled. This means threads running after the HTTP request may be starved of CPU and may be terminated.

If you want background threads to run, select always-on CPU.

How to allocate always-on CPU

  •  Tags:  
  • Related