Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get return value outside visit function? #5

Closed
sukeyisme opened this issue Nov 18, 2021 · 6 comments
Closed

How to get return value outside visit function? #5

sukeyisme opened this issue Nov 18, 2021 · 6 comments

Comments

@sukeyisme
Copy link

No description provided.

@madmongo1
Copy link
Owner

You're going to have to provide some more context to your question.

@sukeyisme
Copy link
Author

net::awaitable< void >
visit_site(http::connection_cache &cache, std::string url)

How get result from this function?

@madmongo1
Copy link
Owner

This function starts a corotuine that "returns" void.

To get it to return a value you'd have to modify the function.

For example:

net::awaitable< response_type >

@sukeyisme
Copy link
Author

But,how to get return value in a nomal function, you get the return in another coroutine.

@sukeyisme
Copy link
Author

net::awaitable< void >
visit_site(http::connection_cache &cache, std::string url)
{
auto then = std::chrono::steady_clock::now();
try
{
auto result = co_await cache.call(http::verb::get, url);
auto time = std::chrono::duration_cast< std::chrono::milliseconds >(
std::chrono::steady_clock::now() - then);
std::cout << "GET " << url << " -> " << result->base().result_int()
<< " " << result->base().reason() << " in " << (time).count()
<< "ms\n";
}
catch (std::exception &e)
{
auto time = std::chrono::duration_cast< std::chrono::milliseconds >(
std::chrono::steady_clock::now() - then);
std::cout << "GET " << url << " -> "
<< " exception: " << e.what() << " in " << (time).count()
<< "ms\n";
}
}

net::awaitable< void >
crawl()
{
// build a connection cache
auto cache = http::connection_cache(co_await net::this_coro::executor);

// Build the async condition variable in order to know when all requests
// have completed
std::size_t pending_count = 0;
auto        cv = net::steady_timer(co_await net::this_coro::executor);
cv.expires_at(net::steady_timer::clock_type::time_point::max());

for (int i = 0; i < 5; ++i)
    for (auto &hostname : urls_large_data())
    {
        auto url = std::string("https://") + hostname + "/";
        ++pending_count;
        net::co_spawn(
            co_await net::this_coro::executor,
            visit_site(cache, std::move(url)),
            [&](std::exception_ptr) {
                if (--pending_count == 0)
                    cv.cancel_one();
            });
    }

std::cout << "waiting for " << pending_count << " requests\n";

while (pending_count)
    co_await cv.async_wait(
        asio::experimental::as_single(net::use_awaitable));

std::cout << "done\n";

}

int
main()
{
net::io_context ioctx;

net::co_spawn(
    ioctx.get_executor(), [] { return crawl(); }, net::detached);

ioctx.run();

}

@madmongo1
Copy link
Owner

This program is written in terms of asynchronous coroutines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants