Tom
Tom

Reputation: 7992

Flask, boost::python and threads

I have a C++ class that is exposed through boost::python:

class RunnerState {
public:
    RunnerState(std::string& input) : m_output(3, 0) {  }
    std::vector<int>& get_output() { return m_output; }
    void run() {
        std::unique_lock<std::mutex> running(m);
        t = new std::thread([this] (std::unique_lock<std::mutex> running) {
            sleep(10);
        }, std::move(running));
    }
    bool is_running() {
        if(m.try_lock()) {
            m.unlock();
            return false;
        }
        return true;
    }
private:
    std::thread *t;
    std::mutex m;
};

BOOST_PYTHON_MODULE(librunner)
{
    class_<std::vector<int>>("int_vector").def(vector_indexing_suite<std::vector<int>>());
    class_<RunnerState, boost::noncopyable>("RunnerState", init<std::string>())
        .def("get_output", &RunnerState::get_output, return_value_policy<copy_non_const_reference>())
        .def("run", &RunnerState::run)
        .def("is_running", &RunnerState::run);
}

This is then used by a Flask webservice:

from flask import Flask, request
import librunner
app = Flask(__name__)
current_run = None

@app.route('/run', methods=['POST'])
def run():
    data = request.get_data()
    current_run = librunner.Run(data)
    output = current_run.get_output()
    # do something with output...
    current_run.run()
    return "Success!", 200, {'Content-Type': 'text/plain'}

@app.route('/running')
def running():
    result = False
    if current_run != None:
        result = current_run.is_running()
    return str(result), 200, {'Content-Type': 'text/plain'}

The problem is that the POST request for /run doesn't return until the thread created in C++ exits. Why?

I'm guessing that this has something to do with the Boost::Python return value policies and Boost::Python keeping the returned values alive for a specified time, but I can't specifically spot what the problem is.

Upvotes: 2

Views: 752

Answers (1)

Tom
Tom

Reputation: 7992

The problem here is that the current_run global is not accessed by the functions, but rather they each get a local called current_run. This is fixed by changing the python to this:

from flask import Flask, request
import librunner
app = Flask(__name__)
current_run = None

@app.route('/run', methods=['POST'])
def run():
    data = request.get_data()
    global current_run
    current_run = librunner.Run(data)
    output = current_run.get_output()
    # do something with output...
    current_run.run()
    return "Success!", 200, {'Content-Type': 'text/plain'}

@app.route('/running')
def running():
    result = False
    global current_run
    if current_run != None:
        result = current_run.is_running()
    return str(result), 200, {'Content-Type': 'text/plain'}

Upvotes: 1

Related Questions