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

Time:03-24

I have a simple send_email_task() function.

@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

I'm calling it in the views like :

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

But I'm facing this error, I don;t understand what it is, I tried reading other answers on stack overflow but not able to find how to handle this problem.

Edit Full traceback

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

CodePudding user response:

When calling apply or apply_async, the arguments to the function must be passed as a list (or a list and a dict for keyword arguments).

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

The error happens because apply tries to use your second parameter as a kwargs dict, and it's actually a string.

  • Related