I am trying to convert a boost::asio::http::async_write call to run in a future. Looking at the use_future documentaion on boost and a few examples I changed my code as follows:
Prior code that works fine
auto bytes_transferred =
boost::beast::http::async_write(*d_tcpStream, req, yield[ec]);
std::cout << "Number of Bytes transmitted: " << bytes_transferred;
Transformed this to:
try{
auto write_fut = boost::beast::http::async_write(*d_tcpStream, req, boost::asio::use_future);
if (write_fut.wait_for(std::chrono::seconds(10)) == std::future_status::timeout){
std::cout << "TIMEOUTTT" << std::endl;
return 1;
}else{
auto bytes_tx = write_fut.get();
std::cout << "Number of Bytes transmitted: " << bytes_tx;
}
}
catch (std::system_error& ex)
{
std::cerr << ex.what() << std::endl;
}
On examining the server on the other side I see no incoming calls being made from my client above. I can see the TIMEOUT prints consistently. It seems like the client after transforming to use use_future does not trigger the write operation altogether.
future::get blocks. You need to have the IO service (e.g. io_context) running in another thread for it to ever complete.
Likely, because you had been using stackful coroutines (asio::spawn) before, you have the io_context::run() somewhere after coro initiation. However, that runs on the main thread, and because no other thread participates in running the IO service you won't make progress since you block the IO service in future::get()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With