folly::coro::AsyncScope

Starts and later joins a dynamic, unbounded set of concurrent tasks.

Synopsis

Declared in <folly/coro/AsyncScope.h>

class AsyncScope;

Description

The AsyncScope class is used to allow you to start a dynamic, unbounded number of tasks, which can all run concurrently, and then later wait for completion of all of the tasks.

Tasks added to an AsyncScope must have a void or folly::Unit result-type and must handle any errors prior to completing.

Example:


folly::coro::Task<void> process(Event event) {
  try {
    co_await do_processing(event.data);
  } catch (...) {
    LOG(ERROR) << "Processing event failed";
  }
}

folly::coro::AsyncScope scope;
scope.add(co_withExecutor(folly::getGlobalCPUExecutor(), process(ev1)));
scope.add(co_withExecutor(folly::getGlobalCPUExecutor(), process(ev2)));
scope.add(co_withExecutor(folly::getGlobalCPUExecutor(), process(ev3)));
co_await scope.joinAsync();

refcode folly/docs/examples/folly/coro/AsyncScope.cpp

Member Functions

NameDescription
AsyncScope [constructor]Constructs an empty scope with no started tasks.
~AsyncScope [destructor]Destroy the AsyncScope object.
add Start the specified task/awaitable by co_awaiting it.
addWithSourceLoc Like add(), but records the call-site source location for diagnostics.
cleanup Asynchronously cleanup all started tasks.
joinAsync Asynchronously wait for all started tasks to complete.
remaining Query the number of tasks added to the scope that have not yet completed.

Non-Member Functions

NameDescription
makeUnorderedAsyncGeneratorYield the results of a vector of awaitables in order of completion.
makeUnorderedAsyncGeneratorYield the results of a range of awaitables in order of completion.
makeUnorderedTryAsyncGeneratorYield Try results of a vector of awaitables in order of completion.
makeUnorderedTryAsyncGeneratorYield Try results of a range of awaitables in order of completion.