django - 运行两个芹菜任务
问题描述
我在 django 中使用 celery,
我在我的项目中添加了一个任务并得到了错误,但是在添加这个任务之前我的项目运行良好。
# 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 runserver运行时得到这个错误
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],
)
推荐阅读
- php - 使用 PHP 从 CSV 中删除一列中有多个类似 ID 的行
- vue.js - 将 OAuth2.0 与 VueJS 一起使用?如何一步一步来?
- java - 如何修复 Time4J 库中的 NoSuchMethodError?
- java - 使用 @Value/SpEL 通过构造函数构建对象列表
- matlab - 在 MATLAB 中查找函数的 CDF
- python - Reportlab生成pdf函数使用
- postgresql - 如何在 PostgreSql 的函数或存储过程中一起返回表和输出参数?
- nginx - 使用 value = 另一个变量的子字符串设置变量
- azure - 事件中心 API POST:40103:授权令牌签名无效
- c# - Roslyn 抛出不支持语言“VisualBasic”