Repository: redis/redis-py
File: tests/test_scenario/test_hitless_upgrade.py (Python)

Code before:

        moving_event = threading.Event()

        def execute_commands(moving_event: threading.Event, errors: Queue):
            while not moving_event.is_set():
                try:
                    client.set("key", "value")
                    client.get("key")
                except Exception as e:
                    errors.put(
                        f"Command failed in thread {threading.current_thread().name}: {e}"
                    )

        logging.info("Starting rebind...")
        bind_thread = Thread(
            target=self._execute_bind,
            name="bind_thread",
            args=(fault_injector_client, endpoints_config, self.endpoint_id),
        )
        bind_thread.start()

        errors = Queue()
        commands_threads = []
        threads_count = 10
        logging.info(f"Starting {threads_count} command execution threads...")
        for _ in range(threads_count):
            thread = Thread(
                target=execute_commands,
                name="command_execution_thread",
                args=(
                    moving_event,
                    errors,
                ),
            )
            thread.start()
            commands_threads.append(thread)

        logging.info("Waiting for MOVING push notification ...")
        # this will consume the notification in one of the connections
        # and will handle the states of the rest
        ClientValidations.wait_push_notification(client, timeout=BIND_TIMEOUT)
        # set the event to stop the command execution threads
        moving_event.set()

        # wait for all command execution threads to stop
        for thread in commands_threads:
            thread.join()

        # validate that all connections are either disconnected
        # or connected to the new address
        connections = self._get_all_connections_in_pool(client)

Review comment (suggestion):
You can use `ThreadPoolExecutor` to run multiple threads, this is more standard way to invoke such operations

Code after:
        migrate_thread.join()

        moving_event = threading.Event()

        def execute_commands(moving_event: threading.Event, errors: Queue):
            while not moving_event.is_set():
                try:
                    client.set("key", "value")
                    client.get("key")
                except Exception as e:
                    errors.put(
                        f"Command failed in thread {threading.current_thread().name}: {e}"
                    )

        logging.info("Starting rebind...")
        bind_thread = Thread(
            target=self._execute_bind,
            name="bind_thread",
            args=(fault_injector_client, endpoints_config, self.endpoint_id),
        )
        bind_thread.start()

        errors = Queue()
        threads_count = 10
        futures = []

        logging.info(f"Starting {threads_count} command execution threads...")
        # Start the worker pool and submit N identical worker tasks
        with ThreadPoolExecutor(
            max_workers=threads_count, thread_name_prefix="command_execution_thread"
        ) as executor:
            futures = [
                executor.submit(execute_commands, moving_event, errors)
                for _ in range(threads_count)
            ]

            logging.info("Waiting for MOVING push notification ...")
            # this will consume the notification in one of the connections
            # and will handle the states of the rest
            ClientValidations.wait_push_notification(client, timeout=BIND_TIMEOUT)
            # set the event to stop the command execution threads
            moving_event.set()

            # Wait for all workers to finish and propagate any exceptions
            for f in futures:
                f.result()

        # validate that all connections are either disconnected
        # or connected to the new address
        connections = self._get_all_connections_in_pool(client)
        for conn in connections: