A simple case to use celery:

Source: Internet
Author: User

Prerequisites:

1:InstallRabbitmqAs it wocould be used as message broker for celery. In Windows, it wocould create a service, make sure the service is started.

2:Install celery: Pip install celery

Meat and potatoes:

Senario 1: Don't specify the backend for celery, if we don't care about the result

1. Create a module namedTasks. py

from __future__ import absolute_importfrom celery import Celeryimport timeapp = Celery(‘tasks‘, broker=‘amqp://[email protected]:5672//‘)@app.taskdef add(x, y):    print ‘hello celery‘    time.sleep(10)    return x + y

 

2. Start celery worker

celery worker -A tasks --loglevel=INFO

You wocould see the console output like below,

 -------------- [email protected] v3.1.10 (Cipater)---- **** -------- * ***  * -- Windows-7-6.1.7601-SP1-- * - **** ---- ** ---------- [config]- ** ---------- .> app:         tasks:0x36871d0- ** ---------- .> transport:   amqp://[email protected]:5672//- ** ---------- .> results:     disabled- *** --- * --- .> concurrency: 8 (prefork)-- ******* ------- ***** ----- [queues] -------------- .> celery           exchange=celery(direct) key=celery[tasks]  . tasks.add[2014-03-26 15:43:11,263: INFO/MainProcess] Connected to amqp://[email protected]:5672//[2014-03-26 15:43:11,285: INFO/MainProcess] mingle: searching for neighbors[2014-03-26 15:43:12,293: INFO/MainProcess] mingle: all alone[2014-03-26 15:43:12,302: WARNING/MainProcess] [email protected] ready.

 

3. Test the Method

Call the function "add ",

>>> from tasks import add>>> result = add.delay(3,5)>>>

You wocould see something like below from celery worker console,

[2014-03-26 15:55:04,117: INFO/MainProcess] Received task: tasks.add[0a52fd72-c7cd-4dc7-91a8-be51f1ff4df2][2014-03-26 15:55:04,118: WARNING/Worker-1] hello celery[2014-03-26 15:55:14,130: INFO/MainProcess] Task tasks.add[0a52fd72-c7cd-4dc7-91a8-be51f1ff4df2] succeeded in 10.0110001564s: 8

If you want to see task status from client, you can use call "result. ready ()". however, as we didn't specify the backend for celery, by defualt it wocould use "disabledbackend", you wocould encounter the following error,

>>> result.ready()Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "C:\Python27\lib\site-packages\celery\result.py", line 254, in ready    return self.state in self.backend.READY_STATES  File "C:\Python27\lib\site-packages\celery\result.py", line 390, in state    return self._get_task_meta()[‘status‘]  File "C:\Python27\lib\site-packages\celery\result.py", line 327, in _get_task_meta    meta = self.backend.get_task_meta(self.id)  File "C:\Python27\lib\site-packages\celery\backends\base.py", line 291, in get_task_meta    meta = self._get_task_meta_for(task_id)AttributeError: ‘DisabledBackend‘ object has no attribute ‘_get_task_meta_for‘

 

To resolve this issue, here comes the following second senario.

 

Senario 2: Specify the backend for celery, if we do care about the result

1. Update the module tasks. py to specify parameter "backend" as "amqp". For other backend specification, refer to Doc

from __future__ import absolute_importfrom celery import Celeryimport timeapp = Celery(‘tasks‘, backend="amqp", broker=‘amqp://[email protected]:5672//‘)@app.taskdef add(x, y):    print ‘hello celery‘    time.sleep(10)    return x + y

2. Restart celery worker and open a new Python shell. (This is important, otherwise the code update above won't take effect)

3. Test

>>> from tasks import add>>> result = add.delay(3,5)>>> result.ready()False>>> result.state‘PENDING‘>>> result.status‘SUCCESS‘>>> result.state‘SUCCESS‘>>> result.ready()True>>> result.get()8>>>

 

See also: https://denibertovic.com/posts/celery-best-practices/

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.