code: def pool_context(*args, **kwargs):
      
      pool = Pool(*args, **kwargs)
      try:
          yield pool
      except Exception as e:
          raise e
      finally:
          pool.terminate()
docs: Context manager for multiprocessing.Pool class (for compatibility with Python 2.7.x)