M
M
Maxim2018-03-15 18:10:32
Django
Maxim, 2018-03-15 18:10:32

How to call a function from task in celery?

There is a function in the directory

app/stats/views.py

@task(name='Sorting response')
def sorting_task_response(*args):
    pass

There are tasks.py
app/api/tasks.py

from __future__ import absolute_import, unicode_literals
# Create your tasks here
import random
import requests
import json
from datetime import datetime
from celery import task, shared_task, Task, group, chord
from stats.views import sorting_task_response


class NotifierTask(Task):
    """
    Tasks that sends notification on completion.
    """
    abstract = True

    def after_return(self, status, retval, task_id, args, kwargs, einfo):
        print(status, retval, task_id, args, kwargs, einfo)
        print('Task done')

def notify_client_ws(token, response):
    data = dict(token=token, notice=response[0])
    url = 'http://ws_server:8060/ws/celery/response'
    resp = requests.post(url, data=json.dumps(data))

# Celery callback
@task(name='CallbackNotifier')
def callback_notifier(*args, **kwargs):
    tasks_response = kwargs.get("tasks_response")
    notify_client_ws(token=kwargs.get("user_token"), response=args)
    return True


@task(name='Data after receiving statistics')
def stats_result(*args, **kwargs):
    sorting_task_response.d(args)

I get an error when importing sorting_task_response
flower_1     |   File "/app/api/tasks.py", line 8, in <module>
flower_1     |     from stats.views import sorting_task_response
flower_1     | ImportError: cannot import name 'sorting_task_response'

How to import a function?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ranc58, 2018-03-15
@Ranc58

from .stats.views import sorting_task_response- so also knocks out an error?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question