简体   繁体   中英

Celery, Group task, AttributeError: 'NoneType' object has no attribute 'app'

I am trying to run a group of celery tasks as follows:

@shared_task
def run_sms_task(smstask_id):

    smstask = SmsTask.objects.get(id = smstask_id)
    if smstask:

        phones = []
        for user in smstask.userlist.users.all():
           phones.append(user.profile.phone)

        g = group(send_sms_async(phone, smstask.text) for phone in phones)
        g.apply_async()

        smstask.status = 3
        smstask.save()

The task is executed partly and in the end throws following error:

[2016-11-01 13:42:03,362: ERROR/MainProcess] Task sms_center.tasks.run_sms_task[d575fb59-6b0a-4ea6-851f-0902ef6bd7b9] raised unexpected: AttributeError("'NoneType' object has no attribute 'app'",)
Traceback (most recent call last):
  File "/home/alexander/.pyenv/versions/d/lib/python3.5/site-packages/celery/app/trace.py", line 240, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/home/alexander/.pyenv/versions/d/lib/python3.5/site-packages/celery/app/trace.py", line 438, in __protected_call__
    return self.run(*args, **kwargs)
  File "/home/alexander/PycharmProjects/d/sms_center/tasks.py", line 25, in run_sms_task
    g.apply_async()
  File "/home/alexander/.pyenv/versions/d/lib/python3.5/site-packages/celery/canvas.py", line 502, in apply_async
    type = self.type
  File "/home/alexander/.pyenv/versions/d/lib/python3.5/site-packages/celery/canvas.py", line 569, in type
    return self.app.tasks[self['task']]
  File "/home/alexander/.pyenv/versions/d/lib/python3.5/site-packages/celery/canvas.py", line 560, in app
    return self._app or (self.tasks[0].app if self.tasks else current_app)
AttributeError: 'NoneType' object has no attribute 'app'

You are welcome to help if you have any ideas. Thank you!

I have found the bug. I didn't notice that Group is created out of Subtasks and not Tasks. The correct group creation would be then:

g = group(send_sms_async.s(phone, smstask.text) for phone in phones)

Hopefully this answer will save some minutes if you have faced the same problem!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM