Repository: django/django
File: django/test/runner.py (Python)

Code before:
        """
        self.initialize_suite()
        counter = multiprocessing.Value(ctypes.c_int, 0)
        pool = multiprocessing.Pool(
            processes=self.processes,
            initializer=self.init_worker.__func__,
            initargs=[
                counter,
                self.initial_settings,
                self.serialized_contents,
                self.process_setup.__func__,
                self.process_setup_args,
                self.debug_mode,
                self.used_aliases,
            ],
        )
        args = [
            (self.runner_class, index, subsuite, self.failfast, self.buffer)
            for index, subsuite in enumerate(self.subsuites)
        ]
        test_results = pool.imap_unordered(self.run_subsuite.__func__, args)

        # Disable buffering on the local test result that will accumulate
        # remote suites results as each process will take care of its own
        # buffering and there's nothing to capture on the main process.
        result.buffer = False

        while True:
            if result.shouldStop:
                pool.terminate()
                break

            try:
                subsuite_index, events = test_results.next(timeout=0.1)
            except multiprocessing.TimeoutError:
                continue
            except StopIteration:
                pool.close()
                break

            tests = list(self.subsuites[subsuite_index])
            for event in events:
                print("tessss", self, event, self.subsuites[subsuite_index])
                self.handle_event(result, tests, event)

        pool.join()

        return result

    def handle_event(self, result, tests, event):
        event_name = event[0]

Review comment (refactor):
I'd move this up, since this could _potentially_ happen before you start running the multiprocessing pool
```suggestion
        # Disable buffering on the local test result that will accumulate
        # remote suites results as each process will take care of its own
        # buffering and there's nothing to capture on the main process.
        result.buffer = False

        test_results = pool.imap_unordered(self.run_subsuite.__func__, args)
```

Code after:
        """
        self.initialize_suite()
        counter = multiprocessing.Value(ctypes.c_int, 0)
        pool = multiprocessing.Pool(
            processes=self.processes,
            initializer=self.init_worker.__func__,
            initargs=[
                counter,
                self.initial_settings,
                self.serialized_contents,
                self.process_setup.__func__,
                self.process_setup_args,
                self.debug_mode,
                self.used_aliases,
            ],
        )
        args = [
            (self.runner_class, index, subsuite, self.failfast, self.buffer)
            for index, subsuite in enumerate(self.subsuites)
        ]
        # Don't buffer in the main process to avoid error propagation issues.
        result.buffer = False

        test_results = pool.imap_unordered(self.run_subsuite.__func__, args)

        while True:
            if result.shouldStop:
                pool.terminate()
                break

            try:
                subsuite_index, events = test_results.next(timeout=0.1)
            except multiprocessing.TimeoutError:
                continue
            except StopIteration:
                pool.close()
                break

            tests = list(self.subsuites[subsuite_index])
            for event in events:
                self.handle_event(result, tests, event)

        pool.join()

        return result

    def handle_event(self, result, tests, event):
        event_name = event[0]
        handler = getattr(result, event_name, None)
        if handler is None:
            return