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.46k stars 889 forks source link

Send response from another thread #351

Open kokonoki opened 4 years ago

kokonoki commented 4 years ago

after receiving request () Try to process DB SP.. Is there any way to do it in other threads by putting it in a queue without doing DB processing immediately?


// Not this way.
CROW_ROUTE(m_lpWebServerBase->m_kCrowApp, "/").methods("POST"_method)([](const crow::request &_request) {
    ....
    std::string test = DB.callSPTestSelect();
    return crow::response(test.c_str());        
});

// Processed in this way
CROW_ROUTE(m_lpWebServerBase->m_kCrowApp, "/").methods("POST"_method)([](const crow::request &_request) {
    ....
    queue.push(...);
    return "";
});

void DBThread()
{
    ...
    getQueue();
    std::string test = DB.callSPTestSelect();
    return crow::response(test.c_str());    
    ...
}
Pipe-Runner commented 4 years ago

@kokonoki Try using boost::asio IO_Service / IO_Context for this... your use case fits the their description perfectly.