Boolean operations: f_or, f_and

Provides AND and OR operations over futures.

Short circuit

Much like a standard boolean expression, these operations are short-circuited where possible.

Futures which are no longer needed in order to calculate the output value will be canceled. In cases where this is not appropriate, consider wrapping the input(s) in f_nocancel().

Example

Consider this Python statement:

result = x() or y() or z()

If x() returns a false value and y() returns a true value, the result of the expression is the returned value of y(), and z() is not evaluated.

Now consider the equivalent with futures:

future = f_or(f_x, f_y, f_z)

All of f_x, f_y and f_z may be running concurrently. If f_y completes first with a true value, f_x and f_z may be cancelled.

If we wanted to allow f_z to continue running even if its output value would not be used, we can instead write:

future = f_or(f_x, f_y, f_nocancel(f_z))
more_executors.f_or(f, *fs)

Boolean OR over a number of futures.

Signature: Future<A>[, Future<B>[, ...]] Future<A|B|...>

Parameters
Returns

Future

A future resolved from the inputs using OR semantics:

  • Resolved with the earliest true value returned by an input future, if any.

  • Otherwise, resolved with the latest false value or exception returned by the input futures.

Note

This function is tested with up to 100,000 input futures. Exceeding this limit may result in performance issues.

New in version 1.19.0.

more_executors.f_and(f, *fs)

Boolean AND over a number of futures.

Signature: Future<A>[, Future<B>[, ...]] Future<A|B|...>

Parameters
Returns

Future

A future resolved from the inputs using AND semantics:

  • Resolved with the latest value returned by an input future, if all futures are resolved with true values.

  • Otherwise, resolved with the earliest false value or exception returned by the input futures.

Note

This function is tested with up to 100,000 input futures. Exceeding this limit may result in performance issues.

New in version 1.19.0.