code: def as_completed(fs, timeout=None):
      """An iterator over the given futures that yields each as it completes.
  
      Args:
          fs: The sequence of Futures (possibly created by different Executors) to
              iterate over.
          timeout: The maximum number of seconds to wait. If None, then there
              is no limit on the wait time.
  
      Returns:
          An iterator that yields the given Futures as they complete (finished or
          cancelled). If any given Futures are duplicated, they will be returned
          once.
  
      Raises:
          TimeoutError: If the entire result iterator could not be generated
              before the given timeout.
      """
      if timeout is not None:
          end_time = timeout + time.monotonic()
  
      fs = set(fs)
      total_futures = len(fs)
      with _AcquireFutures(fs):
          finished = set(
              f
              for f in fs
              if f._state in [CANCELLED_AND_NOTIFIED, FINISHED]
          )
          pending = fs - finished
          waiter = _create_and_install_waiters(fs, _AS_COMPLETED)
      finished = list(finished)
      try:
          yield from _yield_finished_futures(
              finished, waiter, ref_collect=(fs,)
          )
  
          while pending:
              if timeout is None:
                  wait_timeout = None
              else:
                  wait_timeout = end_time - time.monotonic()
                  if wait_timeout < 0:
                      raise TimeoutError(
                          "%d (of %d) futures unfinished"
                          % (len(pending), total_futures)
                      )
  
              waiter.event.wait(wait_timeout)
  
              with waiter.lock:
                  finished = waiter.finished_futures
                  waiter.finished_futures = []
                  waiter.event.clear()
  
              # reverse to keep finishing order
              finished.reverse()
              yield from _yield_finished_futures(
                  finished, waiter, ref_collect=(fs, pending)
              )
  
      finally:
          # Remove waiter from unfinished futures
          for f in fs:
              with f._condition:
                  f._waiters.remove(waiter)
  
imports: import collections
  import logging
  import threading
  import time
  import types
label_desc: Use image node_id 3 for calling a global function with example usage: as_completed(fs, timeout) without return types