[Python] Openface Flask Wrapper: Flask seems to be blocking a thread

Discussão em 'Python' iniciado por Stack, Abril 11, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página