Flask + Celery: working outside of request context
Question: today i started using Celery but am having an issue that am not able to
resolve. more specifically,

my Flask configuration is :

    
    
    from celery import Celery
    def make_celery(app):
        celery = Celery(__name__, broker=app.config['CELERY_BROKER_URL'])
        celery.conf.update(app.config)
        TaskBase = celery.Task
        class ContextTask(TaskBase):
            abstract = True
            def __call__(self, *args, **kwargs):
                with app.app_context():
                    return TaskBase.__call__(self, *args, **kwargs)
        celery.Task = ContextTask
        return celery
    app.config.update(CELERY_BROKER_URL="redis://127.0.0.1:6379", CELERY_RESULT_BACKEND="redis://127.0.0.1:6379", CELERY_IMPORTS=("netdiff"), CELERY_TASK_RESULT_EXPIRES = 300, CELERY_REDIRECT_STDOUTS_LEVEL="DEBUG")
    celery = make_celery(app)
    

I have 1 Celery task declared as :

    
    
    @celery.task(name="implement_netdiff", bind=True)
    def implement_netdiff(diff_):
    

I run a Celery worker as :

    
    
    celery -A netdiff.celery worker
    

but, when i am calling the task :

    
    
    diff_ = {'control': {'maintenance': maintenance,},'netelement': {'host': net element, 'ip': ip, 'pyez': None,},}
    implement_netdiff.apply_async(args=(diff_,))
    

Celery complains about :

    
    
    [2015-06-20 17:23:45,307: ERROR/MainProcess] Task implement_netdiff[714d5cee-b466-4075-9f8c-1b59b745e706] raised unexpected: TypeError('implement_netdiff() takes exactly 1 argument (2 given)',)
    Traceback (most recent call last):
      File "/usr/lib/python2.7/site-packages/celery/app/trace.py", line 240, in trace_task
        R = retval = fun(*args, **kwargs)
      File "/home/app/netdiff/netdiff.py", line 19, in __call__
        return TaskBase.__call__(self, *args, **kwargs)
      File "/usr/lib/python2.7/site-packages/celery/app/trace.py", line 438, in __protected_call__
        return self.run(*args, **kwargs)
    TypeError: implement_netdiff() takes exactly 1 argument (2 given)
    

could you please advise on how this can be resolved ?

EDIT: i changed the declaration of the function to be "def
implement_netdiff(self, diff_):" and it does not complaining anymore.

EDIT: "working outside of request context" was resolved like:

    
    
    def implement_netdiff(self, diff_):
        with celery.app.app_context():
    

QUESTION: what `celery.start()` does ?


Answer: This is
[documented](https://celery.readthedocs.org/en/latest/userguide/tasks.html#task-
request-info).

By specifying `bind=True` you ask celery to bind your method and get a task
type instance as first "`self`" argument.

