运行两个芹菜任务

run two celery task

我在 django 中使用芹菜,
我向我的项目添加了一个任务并出现错误,但是在添加这个任务之前我的项目运行良好。


# app_account.celery_task.py

我的第一个任务是:
@shared_task
def send_birthday_email():
    users = User.objects.filter(is_active=True, date_of_birth__isnull=False, email__isnull=False)
    time = datetime.today()
    for user in users:
        if user.date_of_birth.day == time.day and user.date_of_birth.month == time.month:
            send_mail(
                f'happy birthday',
                f'Happy birthday, dear {user.name}, have a good year',
                'local@host.com',
                [user.email],
            )

我的新任务:

@shared_task
def send_password_reset_mail(mail_info):
    send_mail(
        subject=mail_info['subject'],
        message=mail_info['message'],
        from_email=mail_info['from_email'],
        recipient_list=mail_info['recipient_list'],
    )

此信号中使用的第二个任务:

# this signal for send email reset password
@receiver(reset_password_token_created)
def password_reset_token_created(sender, instance, reset_password_token, *args, **kwargs):
        email_plaintext_message = f"your token is = {reset_password_token.key}"
    
        mail_info = {
            'subject': 'Password Reset',
            'message': email_plaintext_message,
            'from_email': 'noreply@host.com',
            'recipient_list': [reset_password_token.user.email],
        }
        send_password_reset_mail.delay(mail_info)

现在,当我 运行 通过 python manage.py 运行server 得到这个错误


  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/rahmanipy/Desktop/DrfBlog/venv/lib/python3.8/site-packages/django_rest_passwordreset/models.py", line 121, in <module>
    UserModel = get_user_model()
  File "/home/rahmanipy/Desktop/DrfBlog/venv/lib/python3.8/site-packages/django/contrib/auth/__init__.py", line 160, in get_user_model
    return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
  File "/home/rahmanipy/Desktop/DrfBlog/venv/lib/python3.8/site-packages/django/apps/registry.py", line 209, in get_model
    app_config.import_models()
  File "/home/rahmanipy/Desktop/DrfBlog/venv/lib/python3.8/site-packages/django/apps/config.py", line 301, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/rahmanipy/Desktop/DrfBlog/app_account/models.py", line 4, in <module>
    from app_account.celery_tasks import send_password_reset_mail
  File "/home/rahmanipy/Desktop/DrfBlog/app_account/celery_tasks.py", line 4, in <module>
    from app_account.models import User
ImportError: cannot import name 'User' from partially initialized module 'app_account.models' (most likely due to a circular import) (/home/rahmanipy/Desktop/DrfBlog/app_account/models.py)

谁能帮帮我???

您可以在第一个任务中内联导入 User 以避免循环导入。

@shared_task
def send_birthday_email():
    from app_account.models import User
    users = User.objects.filter(is_active=True, date_of_birth__isnull=False, email__isnull=False)
    time = datetime.today()
    for user in users:
        if user.date_of_birth.day == time.day and user.date_of_birth.month == time.month:
            send_mail(
                f'happy birthday',
                f'Happy birthday, dear {user.name}, have a good year',
                'local@host.com',
                [user.email],
            )