keras/src/trainers/data_adapters/py_dataset_adapter.py/PyDatasetAdapter/_make_multiprocessed_generator_fn
class PyDatasetAdapter:    def _make_multiprocessed_generator_fn(self):
        workers = self.py_dataset.workers
        use_multiprocessing = self.py_dataset.use_multiprocessing
        if workers > 1 or (workers > 0 and use_multiprocessing):

            def generator_fn():
                self.enqueuer = OrderedEnqueuer(
                    self.py_dataset,
                    use_multiprocessing=use_multiprocessing,
                    shuffle=self.shuffle,
                )
                self.enqueuer.start(
                    workers=workers,
                    max_queue_size=self.py_dataset.max_queue_size,
                )
                return self.enqueuer.get()

        else:

            def generator_fn():
                num_batches = self.py_dataset.num_batches
                indices = (
                    range(num_batches)
                    if num_batches is not None
                    else itertools.count()
                )
                if self.shuffle and num_batches is not None:
                    # Match the shuffle convention in OrderedEnqueuer.
                    indices = list(indices)
                    random.shuffle(indices)

                for i in indices:
                    yield self.py_dataset[i]

        return generator_fn