Использование многопроцессорной обработки в командном скрипте администрирования django приводит к неожиданной ошибке

Я пытаюсь создать пользовательскую команду администрирования в моем проекте django. Я начал использовать concurrent.futures.ThreadPoolExecutor() для параллельного выполнения моего кода. Это работает хорошо, как показано здесь:

def my_function(param_1=None):
# this function needs to be executed parallely

class Command(BaseCommand):

    def handle(self, *args, **options):
        status = True
        while status:
            my_model_data = my_model.objects.exclude(
                some_id__isnull=True
            ).exclude(
              some_id__exact='0'
            ).exclude(
              other_id__exact=0
            ).filter(
                status="pending"
            )[0:2]

            if not my_model_data:
                continue

            with concurrent.futures.ThreadPoolExecutor() as executor:
                futures = []
                for row_data in my_model_data:
                    futures.append(
                        executor.submit(
                            my_function, param_1=row_data
                        )
                    )
                for future in concurrent.futures.as_completed(futures):
                    try:
                        print(future.result())
                    except Exception as e:
                        print(e)

Я искал другие альтернативы для этого, где я попробовал следующие 2 подхода:

Подход 1:

            with Pool() as p:
                try:
                    for row_data in my_model_data:
                        p.starmap(
                            my_function,
                            list(row_data)
                        )
                except Exception as e:
                    print(e)

Подход 2 ( Отсюда):

def subprocess_setup():
    django.setup()
            with ProcessPoolExecutor(max_workers=5, initializer=subprocess_setup) as executor:
                try:
                    for row_data in my_model_data:
                        executor.map(
                            my_function,
                            list(row_data)
                        )
                except Exception as e:
                    print(e)

При обоих этих подходах я получаю следующую ошибку: 'my_model' object is not iterable

Любая помощь была бы замечательной!

Вернуться на верх