d33tah
d33tah

Reputation: 11569

Why does this Celery "hello world" loop forever?

Consider the code:

from celery import Celery, group
from time import time

app = Celery('tasks', broker='redis:///0', backend='redis:///1', task_ignore_result=False)

@app.task
def test_task(i):
    print('hi')
    return i

x = test_task.delay(3)
print(x.get())

I run it by calling python script.py, but I'm getting no results. Why?

Upvotes: 0

Views: 1926

Answers (1)

rileymcdowell
rileymcdowell

Reputation: 599

You don't get any results because you've asked your celery app to execute a task without starting a worker process to do the work executing it. The process you did start is blocked on the call to get().

First things first, when using celery it is critical that you do not have tasks get executed when a module is imported, so let's put your task execution inside of a main() function, and put it in a file called celery_test.py.

from celery import Celery, group
from time import time

app = Celery('tasks', broker='redis:///0', backend='redis:///1', task_ignore_result=False)

@app.task
def test_task(i):
    print('hi')
    return i

def main():
    x = test_task.delay(3)
    print(x.get())

if __name__ == '__main__':
    main()

Now let's start a pool of celery workers to execute tasks for this app. You can do this by opening a new terminal and executing the following.

celery worker -A celery_test --loglevel=INFO

The -A flag refers to the module where celery will find an application to add workers to. You should see some output in the terminal to indicate that the the celery worker is running and ready for tasks to process.

Now, try executing your script again with python celery_test.py. You should see hi show up in the worker's log output, but the the value 3 returned in the script that called get().

Be warned, if you've been playing with celery without running a worker, it probably has lots of tasks waiting in your broker to execute. The first time you start up the worker pool, you'll see them all execute in parallel until the broker runs out of tasks.

Upvotes: 3

Related Questions