class tensorstore.Future

Handle for consuming the result of an asynchronous operation.

This type supports several different patterns for consuming results:

  • Asynchronously with asyncio, using the await keyword:

    >>> future = ts.open({
    ...     'driver': 'array',
    ...     'array': [1, 2, 3],
    ...     'dtype': 'uint32'
    ... })
    >>> await future
    TensorStore({
      'array': [1, 2, 3],
      'driver': 'array',
      'dtype': 'uint32',
      'transform': {'input_exclusive_max': [3], 'input_inclusive_min': [0]},
    })
    
  • Synchronously blocking the current thread, by calling result().

    >>> future = ts.open({
    ...     'driver': 'array',
    ...     'array': [1, 2, 3],
    ...     'dtype': 'uint32'
    ... })
    >>> future.result()
    TensorStore({
      'array': [1, 2, 3],
      'driver': 'array',
      'dtype': 'uint32',
      'transform': {'input_exclusive_max': [3], 'input_inclusive_min': [0]},
    })
    
  • Asynchronously, by registering a callback using add_done_callback():

    >>> future = ts.open({
    ...     'driver': 'array',
    ...     'array': [1, 2, 3],
    ...     'dtype': 'uint32'
    ... })
    >>> future.add_done_callback(
    ...     lambda f: print(f'Callback: {f.result().domain}'))
    ... future.force()  # ensure the operation is started
    ... # wait for completion (for testing only)
    ... result = future.result()
    Callback: { [0, 3) }
    

If an error occurs, instead of returning a value, result() or python:await will raise an exception.

This type supports a subset of the interfaces of concurrent.futures.Future and asyncio.Future. Unlike those types, however, Future provides only the consumer interface. The corresponding producer interface is provided by Promise.

See also

Public members

add_done_callback(callback: object)None
remove_done_callback(callback: object)int
result(timeout: float | None = None, )object
exception(timeout: float | None = None, )object
done()bool
force()None
cancelled()bool
cancel()bool