V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
gamexg
V2EX  ›  Python

Celery 无法获得任务结果,状态总是 PENDING 。ignore_result = False 等文档上的办法都试过了还是无效...

  •  
  •   gamexg ·
    gamexg · 2015-03-17 21:07:01 +08:00 · 12594 次点击
    这是一个创建于 3331 天前的主题,其中的信息可能已经有所发展或是发生改变。

    测试环境是windows 7 64
    python 2.7.8
    Celery 3.2
    Redis 2.8.19

    代码就是 Celery 的例子

    https://gist.github.com/GameXG/21763336458d417a60ad

    Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win32
    》 sys.path.append(r".\TaskQueue")
    》 from tasks import add
    》 r = add.delay(5,10)
    》 r.status
    'PENDING'
    》 r.status
    'PENDING'
    》 r.ready()
    False
    状态总是 PENDING ,tasks worker 明明显示任务已经完成了。但总是得不到任务结果,文档上面的几种办法都尝试过了,还是无效...

    也尝试过用两个 redis 分开试,结果一样无任务结果...

    redis-cli 得到的数据库内容:
    127.0.0.1:6379> keys *
    1) "_kombu.binding.celeryev"
    2) "_kombu.binding.celery"
    3) "_kombu.binding.celery.pidbox"

    tasks worker工作日志

    c:\Python27\Scripts\celery.exe -A tasks worker --loglevel=info

    -------------- celery@GameXG-PC v3.1.17 (Cipater)
    ---- **** -----
    --- * *** * -- Windows-7-6.1.7601-SP1
    -- * - **** ---
    - ** ---------- [config]
    - ** ---------- .> app: tasks:0x2ff2550
    - ** ---------- .> transport: redis://localhost:6379//
    - ** ---------- .> results: redis://localhost/
    - *** --- * --- .> concurrency: 4 (prefork)
    -- ******* ----
    --- ***** ----- [queues]
    -------------- .> celery exchange=celery(direct) key=celery

    [tasks]
    . tasks.add

    [2015-03-17 20:53:36,552: INFO/MainProcess] Connected to redis://localhost:6379//
    [2015-03-17 20:53:36,605: INFO/MainProcess] mingle: searching for neighbors
    [2015-03-17 20:53:37,625: INFO/MainProcess] mingle: all alone
    [2015-03-17 20:53:37,638: WARNING/MainProcess] celery@GameXG-PC ready.
    [2015-03-17 20:53:46,365: INFO/MainProcess] Received task: tasks.add[9aaa35f7-f1f1-4360-add4-37467aa2d770]
    [2015-03-17 20:53:46,377: INFO/MainProcess] Task tasks.add[9aaa35f7-f1f1-4360-add4-37467aa2d770] succeeded in 0.00999999046326s: 15

    10 条回复    2018-09-05 18:47:33 +08:00
    20150517
        1
    20150517  
       2015-03-17 21:39:13 +08:00
    add.apply_async(...)
    gamexg
        2
    gamexg  
    OP
       2015-03-17 21:43:52 +08:00
    相同代码在 ubuntu 下测试可以正常工作。

    调用内部方法也可以正常保存:
    》 r.backend._store_result(r.id,"11","00","00")
    '11'
    》 r.status
    u'00'
    》 r.result
    u'11'

    可以看到这次数据库里面有了内容:
    127.0.0.1:6379> keys *
    1) "_kombu.binding.celeryev"
    2) "celery-task-meta-00000"
    3) "celery-task-meta-21b7472a-88b7-4850-8659-893ceaa18973"
    4) "_kombu.binding.celery"
    5) "_kombu.binding.celery.pidbox"
    gamexg
        3
    gamexg  
    OP
       2015-03-17 21:45:55 +08:00
    @20150517 同样无效...

    》 r=add.apply_async(args=(1,2))
    》 r.result
    》 r.ready()
    False
    20150517
        4
    20150517  
       2015-03-17 21:52:18 +08:00 via Android
    你backend设了没有
    gamexg
        5
    gamexg  
    OP
       2015-03-17 21:58:36 +08:00
    @20150517 设置了 app = Celery('tasks', backend='redis://localhost', broker='redis://localhost')
    20150517
        6
    20150517  
       2015-03-17 22:19:19 +08:00 via Android
    purge下 重启下redis呢 celery这东西很怪的 有时候我明明有task就是不跑
    x14oL
        8
    x14oL  
       2015-05-22 01:27:08 +08:00
    @gamexg 昨天拿你的解决了问题,现在发现你的解决方案不是最优的。
    --pool=solo会把程序变成单线程的,这样效率太低了。
    这时候应该切换模式。
    使用-P threads 模式就可以解决这个问题了。
    celery.exe -A tasks worker -l info -P threads
    类似这样。
    akmonde
        9
    akmonde  
       2017-05-17 16:59:21 +08:00
    @gamexg @x14oL 遇到了类似的问题,不过鄙人是在 linux 下,使用了下面的 code:
    while not result.ready()
    这里采用的循环的形式,监听任务是否结束,结果一直是 PENDING,形成了死循环。任务是一直没起跑,但是任务函数本身是没有问题的。
    我试过-P threads,结果在调用 billiard 时会报错:
    AttributeError: 'WorkerThread' object has no attribute '_children'
    --pool=solo 参数是压根不生效,
    求大佬解答。。
    thechosenone
        10
    thechosenone  
       2018-09-05 18:47:33 +08:00
    虽然时间过去久远,但是可能还会有人一样和我出现同样的问题而点进来找答案的
    我的情况和 lz 一样,解决办法重启 redis 就可以
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   941 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 21:51 · PVG 05:51 · LAX 14:51 · JFK 17:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.