Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery scheduled list returns None

I'm fairly new to Celery and I've been attempting setup a simple script to schedule and unschedule tasks. However I feel like I'm running into a weird issue. I have the following setup

from celery import Celery
app = Celery('celery_test',
             broker='amqp://',
             backend='amqp')

@app.task
def add(x, y):
    return x + y

I start up my celery server just fine and can add tasks. Now when I want to get a list of active tasks things seem to get weird. When I goto use inspect to get a list of scheduled tasks it works exactly once then returns None every time afterwards.

>>> i = app.control.inspect()
>>> print i.scheduled()
{u'celery@mymachine': []}
>>> print i.scheduled()
None
>>>

This happens whether I add tasks or not. I want to find a way to consistently return a list of tasks from my celery queue. I want to do this so I can find a previously queued task, revoke it, and reschedule it. I feel like I'm missing something basic here.

like image 954
The Jug Avatar asked Jun 16 '14 03:06

The Jug


2 Answers

To repeat call to get list of tasks in queues you have to create new instance of Celery object. I was trying to figure out why it's necessary by debugging code executed by calling ./manage.py celery inspect scheduled but without any luck. Maybe someone will have more experience with that and add some additional informations to this answer.

Try this simple snippet for checking list of scheduled tasks:

from celery import Celery

def inspect(method):
    app = Celery('app', broker='amqp://')
    return getattr(app.control.inspect(), method)()

print inspect('scheduled')
print inspect('active')
like image 56
daniula Avatar answered Sep 28 '22 07:09

daniula


Thanks to daniula,

I'm using this code in django-celery-rabbitmq and i need to close app instance aftert inspect... like this:

from celery import Celery

def inspect(method):
    app = Celery('app', broker='amqp://')
    inspect_result = getattr(app.control.inspect(), method)()
    app.close()
    return inspect_result

print inspect('scheduled')
print inspect('active')

In my case if i don't call app.close() socket connection to rabbitmq still alive (active), in this way all socket descriptors will be consumed and after that, new socket connection cannot be available, so everything stop to work.

like image 33
Pierluigi Felici Avatar answered Sep 28 '22 07:09

Pierluigi Felici