cofetch
Chainable, high-performance async HTTP client for C++ event loops
Loading...
Searching...
No Matches
example_concurrent.cpp

Twenty requests in flight at once on a single thread.

Twenty requests in flight at once on a single thread.

// Twenty requests in flight at once — one thread, no pool. The event
// loop multiplexes them; completions arrive as each response lands.
#include <cofetch.h>
#include <asio.hpp>
#include <iostream>
#include <string>
int main() {
asio::io_context io;
cofetch::Client http(io);
int done = 0;
for (int i = 1; i <= 20; ++i) {
http.async_get(
"https://postman-echo.com/get?n=" + std::to_string(i),
[&done, i](std::error_code ec, const cofetch::Response& res) {
std::cout << "#" << i << " -> "
<< (ec ? ec.message() : std::to_string(res.http_code_))
<< "\n";
++done;
});
}
io.run();
std::cout << done << " requests completed\n";
}
Definition cofetch.h:266
auto async_get(std::string url, CompletionToken &&token)
Definition cofetch.h:324
Definition cofetch.h:111
long http_code_
Definition cofetch.h:176