** 之后的参数必须是一个映射,而不是 str 错误,使用 celery 但不是不使用它

argument after ** must be a mapping, not str error, using celery but not without using it

我有一个简单的 send_email_task() 函数。

@shared_task
def send_email_task(subject, message, email_from, recipient_list):
    print("Email Sent", f"{subject, message, email_from, recipient_list}")
    send_mail(subject, message, email_from, recipient_list)

    return None

我在这样的视图中调用它:

send_email_task.apply_async(
                    subject, message, email_from, recipient_list)

但是我遇到了这个错误,我不明白它是什么,我尝试阅读有关堆栈溢出的其他答案,但找不到如何处理这个问题。

编辑 完整追溯

Internal Server Error: /api/v1/accounts/user/password/change/
Traceback (most recent call last):
  File "\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "\venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "\venv\lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "\venv\lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "\venv\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "\venv\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "\venv\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "\venv\lib\site-packages\rest_framework\decorators.py", line 50, in handler
    return func(*args, **kwargs)
  File "\accounts\views.py", line 268, in password_change
    send_email_task.apply_async(
  File "\venv\lib\site-packages\celery\app\task.py", line 540, in apply_async
    check_arguments(*(args or ()), **(kwargs or {}))
TypeError: accounts.tasks.send_email_task() argument after ** must be a mapping, not str

当调用 applyapply_async 时,函数的参数必须作为列表(或关键字参数的列表和字典)传递。

send_email_task.apply_async(
    [subject, message, email_from, recipient_list]
)

错误发生是因为 apply 试图将您的第二个参数用作 kwargs dict,而它实际上是一个字符串。