A simple case to use Celery:

Prerequisites: 

 1: Install RabbitMQ as it would be used as message broker for Celery. In windows, it would 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 named tasks.py

from __future__ import absolute_import
from celery import Celery
import time

app = Celery(‘tasks‘, broker=‘amqp://[email protected]:5672//‘)

@app.task
def add(x, y):
    print ‘hello celery‘
    time.sleep(10)
    return x + y

2. Start Celery worker

celery worker -A tasks --loglevel=INFO

You would 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 would 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 would use "DisabledBackend", you would 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_import
from celery import Celery
import time

app = Celery(‘tasks‘, backend="amqp", broker=‘amqp://[email protected]:5672//‘)

@app.task
def 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/

A simple case to use Celery:

时间: 2024-09-29 23:34:20

A simple case to use Celery:的相关文章

Ettus Research USRP B200/B210 simple case

hdu 4975 A simple Gaussian elimination problem.(网络流,判断矩阵是否存在)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 Problem Description Dragon is studying math. One day, he drew a table with several rows and columns, randomly wrote numbers on each elements of the table. Then he counted the sum of each row and col

CASE (Transact-SQL)

A. 使用带有 CASE 简单表达式的 SELECT 语句Using a SELECT statement with a simple CASE expression在 SELECT 语句中,CASE 简单表达式只能用于等同性检查,而不进行其他比较. 下面的示例使用 CASE 表达式更改产品系列类别的显示,以使这些类别更易于理解. USE AdventureWorks2012; GO SELECT ProductNumber, Category = CASE ProductLine WHEN '

Simple Expert Advisor

https://book.mql4.com/samples/expert WebTerminal Documentation Book TA Code Base Articles Freelance Market Signals VPS Forum Login MQL4 Book  Simple Programs in MQL4  Simple Expert Advisor Usage of Technical Indicators Simple Expert Advisor Creation

case when语句后的表达式

SQL中Case When语句的语法如下 Simple CASE expression: CASE input_expression WHEN when_expression THEN result_expression [ ...n ] [ ELSE else_result_expression ] END Searched CASE expression: CASE WHEN Boolean_expression THEN result_expression [ ...n ] [ ELSE

HDU 4975 A simple Gaussian elimination problem.(网络最大流)

http://acm.hdu.edu.cn/showproblem.php?pid=4975 A simple Gaussian elimination problem. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 669    Accepted Submission(s): 222 Problem Description Drag

HDOJ 4975 A simple Gaussian elimination problem.

和HDOJ4888是一样的问题,最大流推断多解 1.把ISAP卡的根本出不来结果,仅仅能把全为0或者全为满流的给特判掉...... 2.在残量网络中找大于2的圈要用一种类似tarjian的方法从汇点開始找,推断哪些点没有到汇点 A simple Gaussian elimination problem. Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submiss

A simple Gaussian elimination problem.(hdu4975)网络流+最大流

A simple Gaussian elimination problem. Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 728 Accepted Submission(s): 241 Problem Description Dragon is studying math. One day, he drew a table with sev

hdu 4975 A simple Gaussian elimination problem.(网络流,推断矩阵是否存在)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 Problem Description Dragon is studying math. One day, he drew a table with several rows and columns, randomly wrote numbers on each elements of the table. Then he counted the sum of each row and col