code: def run_parallel(
      execute: Callable[[T], U], data: List[T], num_processes: int
  ) -> List[U]:
      """
      Runs a function on multiple processes, parallelizing computations for the
      provided data.
  
      Args:
          execute: The function to run in each process.
          data: The data items to execute the function for.
          num_processes: The number of processes to parallelize over.
  
      Returns:
          The outputs of the function calls, ordered in the same way as the data.
      """
  
      def factory(_i: int) -> mp.Process:
          process = mp.Process(
              target=_worker, args=(execute, inputs, outputs)
          )
          process.start()
          return process
  
      # We share data via the global data cache such that we do not need to pass too much data over
      # queues -- forking allows to read the same data from multiple processes without issues.
      global _GLOBAL_DATA_CACHE  # pylint: disable=global-statement
      _GLOBAL_DATA_CACHE = data  # type: ignore
  
      # Initialize queues and put all items into input queue. Also, put as many "done" items into the
      # queue as we have processes
      inputs = mp.Queue()
      outputs = mp.Queue()
      for i in range(len(data)):
          inputs.put(i)
  
      # Create the processes in a thread pool to speed up creation
      with ThreadPool(num_processes) as p:
          processes = p.map_async(factory, range(num_processes))
  
          # Parallelize execution -- keep this inside the with statement so processes keep
          # getting spawned
          result = [None] * len(data)
          with tqdm(total=len(data)) as progress:
              progress.set_postfix({"num_processes": num_processes})
              for _ in range(len(data)):
                  i, output = outputs.get()
                  result[i] = output
                  progress.update()
  
          for p in processes.get():
              p.kill()
  
      # cleanup
      inputs.close()
      outputs.close()
      return cast(List[U], result)
  
file_path: gluonts/src/gluonts/nursery/tsbench/src/tsbench/analysis/utils/multiprocessing.py
imports: import math
  import multiprocessing
  import os
  from multiprocessing.pool import ThreadPool
  from queue import Empty
  from typing import Any, Callable, cast, List, TypeVar
  import psutil
  from tqdm.auto import tqdm
label_desc: Use image node_id 2 for calling a global function with example usage: run_parallel(execute, data, num_processes) and returns: cast, process