我的芹菜从 amqp 开始
-------------- celery@tty-Gazelle-Professional v3.0.19 (Chiastic Slide)
---- **** -----
--- * *** * -- Linux-3.8.0-25-generic-x86_64-with-Ubuntu-13.04-raring
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> broker: amqp://guest@localhost:5672//
- ** ---------- .> app: proj.celery:0x25ed510
- ** ---------- .> concurrency: 8 (processes)
- *** --- * --- .> events: OFF (enable -E to monitor this worker)
-- ******* ----
--- ***** ----- [queues]
-------------- .> celery: exchange:celery(direct) binding:celery
有一个功能:
def prime(n):
.....
.....
return number_of_primes_below_n
所以我把这个函数作为芹菜中的一项任务,并与串行计算进行比较
连载:
[prime(i) for i in xrange(10, 100000)]
与芹菜的平行:
from celery import *
g = group(prime.s(i) for i in xrange(10, 100000))
res = g.apply_async()
当我 apply_async() 时,在后端会很快在终端屏幕上显示结果,例如:
[2013-06-20 16:34:56,238: INFO/MainProcess] Task proj.tasks.do_work[989be06b-c4f3-4876-9311-2f5f813857d5] succeeded in 0.0166230201721s: 99640324
[2013-06-20 16:34:56,241: INFO/MainProcess] Task proj.tasks.do_work[6eaa9b85-7ba2-4397-b6ae-cbb5668633d4] succeeded in 0.0123620033264s: 99740169
[2013-06-20 16:34:56,242: INFO/MainProcess] Task proj.tasks.do_work[1f5f6302-94a3-4937-9914-14690d856a5d] succeeded in 0.00850105285645s: 99780121
[2013-06-20 16:34:56,244: INFO/MainProcess] Task proj.tasks.do_work[b3735842-a49c-48a3-8a9e-fab24c0a6c23] succeeded in 0.0102620124817s: 99820081
[2013-06-20 16:34:56,245: INFO/MainProcess] Task proj.tasks.do_work[98eec31a-52eb-4752-92af-6956c0e6f130] succeeded in 0.00973200798035s: 99880036
[2013-06-20 16:34:56,245: INFO/MainProcess] Task proj.tasks.do_work[011a1e99-b307-480b-9765-b1a472dbfa8c] succeeded in 0.0115168094635s: 99800100
[2013-06-20 16:34:56,245: INFO/MainProcess] Task proj.tasks.do_work[f3e3a89f-de79-4ab0-aab7-0a71fe2ab2f7] succeeded in 0.010409116745s: 99840064
[2013-06-20 16:34:56,246: INFO/MainProcess] Task proj.tasks.do_work[61baef04-03c2-4810-bf6a-ae7aa75b80b4] succeeded in 0.0112910270691s: 99860049
但是当我想用 celery 得到结果时
res.get()
它运行非常非常慢,比串行慢得多。问题是什么?是不是因为 celery group 的结果比较慢?我该如何解决这个问题?