code: def extract_knn(knn_index, verbose=1, n_jobs=-1):
      """Starts multiple threads to retrieve nearest neighbours from a built index in parallel.
  
      :param `NeighbourMatrix` knn_index: Indexable neighbour index. When indexed, returns
          a list of neighbour indices for that row of the dataset it was built on.
      :param int verbose: Controls verbosity of output.
      """
  
      if verbose:
          print("Extracting KNN neighbours")
  
      nrows = len(knn_index)
      try:
          neighbour_matrix = np.empty(
              (nrows, knn_index.k), dtype=get_uint_ctype(nrows)
          )
      except (ValueError, MemoryError) as err:
          raise MemoryError(
              "Unable to allocate memory for precomputed KNN matrix. "
              "Set `precompute` to False or reduce the value for `k`."
          ) from err
  
      worker_exception = None  # Halt signal
  
      def knn_worker(thread_index, data_indices):
          nonlocal worker_exception
          for i in range(data_indices[0], data_indices[1]):
              try:
                  if worker_exception:
                      break
                  neighbour_matrix[i] = knn_index[i]
                  progress_counters[thread_index] += 1
              except Exception as e:
                  worker_exception = e
  
      # Split up the indices and assign a thread for each chunk
      cpus_available = os.cpu_count() or 1
      n_jobs = n_jobs if n_jobs > 0 else cpus_available + n_jobs + 1
      chunk_size = max(1, nrows // n_jobs)
      data_split_indices = [
          (min_index, min(nrows, min_index + chunk_size))
          for min_index in range(0, nrows, chunk_size)
      ]
  
      progress_counters = [0 for _ in range(len(data_split_indices))]
      thread_pool = [
          Thread(
              target=knn_worker,
              kwargs={
                  "thread_index": i,
                  "data_indices": (min_index, max_index),
              },
          )
          for i, (min_index, max_index) in enumerate(data_split_indices)
      ]
  
      try:
          for thread in thread_pool:
              thread.start()
  
          num_processed = 0
          # Poll for progress updates
          with tqdm(total=nrows, disable=verbose < 1) as pbar:
              while num_processed < nrows:
                  # Raise worker errors
                  if worker_exception:
                      raise worker_exception
  
                  progress = sum(progress_counters) - num_processed
                  num_processed += progress
                  pbar.update(progress)
                  time.sleep(0.1)
  
          return neighbour_matrix
  
      except BaseException as e:
          print(
              "Error encountered. Halting KNN retrieval and cleaning up"
          )
          # Signal worker threads to terminate
          worker_exception = e
          for thread in thread_pool:
              thread.join()
          raise
  
imports: from concurrent.futures import ThreadPoolExecutor
  import os
  import shutil
  import time
  from abc import abstractmethod
  from threading import Thread
  from collections.abc import Sequence
  from pathlib import Path
  from scipy.sparse import issparse
  from annoy import AnnoyIndex
  from tqdm import tqdm
  import numpy
  from ..data import get_uint_ctype
label_desc: Use image node_id 3 for calling a global function with example usage: extract_knn(knn_index, verbose, n_jobs) and returns: neighbour_matrix