def run_tasks(
    tasks: Mapping[str, Callable[[], _T]],
    *,
    timeout: float | None = None,
    max_workers: int | None = None,
) -> Mapping[str, _T]:
  """Runs the callables in parallel, blocks until first failure.

  IMPORTANT: Passed callables must be threadsafe.

  Args:
    tasks: callables to execute (MUST BE THREADSAFE)
    timeout: the maximum number of seconds to wait.
    max_workers: them maximum number of parallel jobs. If None will use as many
      workers as there are tasks.

  Returns:
    The results fn(*arg) for arg in args]
    However, the calls will be executed concurrently.

  Raises:
    TimeoutError: If all the results are not generated before the timeout.
    Exception: If any task raises an exception.
  """
  return {
      key: future.result()
      for key, future in _as_completed(
          tasks, timeout=timeout, max_workers=max_workers
      )
  }
Runs the callables in parallel, blocks until first failure.

  IMPORTANT: Passed callables must be threadsafe.

  Args:
    tasks: callables to execute (MUST BE THREADSAFE)
    timeout: the maximum number of seconds to wait.
    max_workers: them maximum number of parallel jobs. If None will use as many
      workers as there are tasks.

  Returns:
    The results fn(*arg) for arg in args]
    However, the calls will be executed concurrently.

  Raises:
    TimeoutError: If all the results are not generated before the timeout.
    Exception: If any task raises an exception.
  
concordia/utils/concurrency.py