ipkn / crow

Crow is very fast and easy to use C++ micro web framework (inspired by Python Flask)
BSD 3-Clause "New" or "Revised" License
7.47k stars 888 forks source link

How to Send Async Response #309

Closed gunnxx closed 6 years ago

gunnxx commented 6 years ago

How to send async response for exact certain request?

int main() {
    crow::SimpleApp app;

    std::queue<Request> _requests;

    CROW_ROUTE(app, "/<string>").methods("POST"_method)([]
                (const crow::request &req,
                 const crow::response &res,
                 std::string method){
        auto data = crow::json::load(req.body);

        Request request;
        request.method = method;
        request.ip = data["ip"];
        ........
        _requests.push(request);
    });

    app.port(8000).multithreaded().run();
}

Later, I will have a thread that consume the queue and run the requested method. How can the thread send back the response to the exact request?

pierobot commented 6 years ago

You will need to have a thread or thread pool that consumes the queue and when one of those queue objects have completed call a lambda inside the route.

It's not quite what you're asking for but you can see what I mean by looking at the second code section here.

So your route needs to return void and the on_ready lambda needs to call response::write to write some data to the response and call response::end to signal that the response is ready to be sent.