Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Openface Flask Wrapper: Flask seems to be blocking a thread

I am trying to write a small flask REST API wrapper around the openface api so that I can POST image URLs to my flask server and have it run a comparison of the image against a classifier model

app = Flask(__name__)
@app.route('/compare', methods=['POST'])
def compare():
    # create arguments object with default classifier and neural net
    args = CompareArguments(image)
    image = request.json['image']
    args.imgs = image
    align = openface.AlignDlib(args.dlibFacePredictor)
    net = openface.TorchNeuralNet(args.networkModel, imgDim=args.imgDim, cuda=args.cuda)
    # call openface and compare image to classifier
    infer(args, align, net)
    return jsonify({'image': image}), 201

if __name__ == '__main__':
    app.run(host='0.0.0.0', threaded=True)

If I POST an image like so

curl -i -H "Content-Type: application/json" -X POST http://localhost:5000/compare -d '{"image": [ "../images/examples/clapton-1.jpg"]}'

A new torch process is created and can be seen in the output from ps -aux, but seems to be blocked, as it doesn't run until the server is reloaded

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  18184  3284 ?        Ss   18:46   0:00 /bin/bash
root       188  3.5  2.4 676060 98320 ?        S    19:35   0:00 python ./app.py
root       197 98.7  1.5 202548 62388 ?        R    19:35   0:08 /root/torch/install/bin/luajit -e package.path="/root/.luarocks/share/lua/5.1/?.lua;/root/.luarocks/share/lua/5.1/?/init.lua;/root/torch/install
root       211 39.2  1.5 202548 60908 ?        R    19:36   0:01 /root/torch/install/bin/luajit -e package.path="/root/.luarocks/share/lua/5.1/?.lua;/root/.luarocks/share/lua/5.1/?/init.lua;/root/torch/install

It seems like the torch process is being blocked by flask somehow? I have enabled threading and have tried increasing the number of processes. I'm not sure what could be blocking this process? Is there some way I can debug this or extra config required for threading in Flask?

like image 263
Brien Crean Avatar asked Jan 31 '26 04:01

Brien Crean


2 Answers

So an issue with flask is that it uses threads itself, so ur going to need to turn off threading if you want it to work. Please use this:

if __name__ == '__main__':
    app.run(host='0.0.0.0', threaded=False)
like image 163
Mohit Reddy Avatar answered Feb 01 '26 21:02

Mohit Reddy



Flask blocks because OpenFace processing is slow and runs in the same thread. Use Python's threading module to run it separately:


from flask import Flask, request, jsonify

import threading

import time

app = Flask(\__name_\_)

def process_image(image_url):

    time.sleep(5)  # Simulate OpenFace processing
    
    return {"result": "processed"}

@app.route('/compare', methods=\['POST'\])

def compare():

    data = request.json
    
    image_url = data.get('image')
    
    result = {}
    
    def run_processing():
    
        nonlocal result
    
        result = process_image(image_url)
    
    thread = threading.Thread(target=run_processing)
    
    thread.start()   
    return jsonify({"message": "Processing started"})
like image 30
Damon33 Avatar answered Feb 01 '26 21:02

Damon33