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
40huo
V2EX  ›  Python

celery 执行自定义 inspect command 的问题

  •  
  •   40huo · 2019-09-25 16:22:58 +08:00 · 2045 次点击
    这是一个创建于 1691 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有个项目需要用到后台任务,选了 celery,但在一个场景下遇到了问题。

    需要定时收集所有 worker 的一些信息,比如 CPU 负载之类,相当于一个广播出去的任务,所有 worker 执行一下然后将结果回传,但 celery 中的 broadcast task 由于 taskid 是一样的,结果只能收到一个。

    celery 提供了另外一种远程控制 worker 的方式,celery inspect xxx,可以自定义一个inspect_command来执行,这在命令行下是可以正常使用,但在定时任务中遇到了问题。

    @inspect_command()
    def resource_usage(state):
        result = get_resource_usage()
        result = result.__dict__
        logger.warning(result)
        return {"a": result}
    
    
    # 这是个定时任务,比如每分钟收集一次
    @shared_task()
    def get_worker_stats():
        from tasks import app
    
        i = app.control.inspect()
        results = i.resource_usage()
        logger.info(results)
        # 其他逻辑,存入 DB 等
    

    直接执行 celery -A tasks inspect resource_usage 可以正常运行,但在定时任务中运行时会出现

    AttributeError: 'Inspect' object has no attribute 'resource_usage'
    

    这样的错误,似乎是自定义的 command 没有注册,但文档里也没有提到如何强制注册。。。

    这种情况应该如何处理,如果 celery 实现不了别的库也行。

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3772 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 05:03 · PVG 13:03 · LAX 22:03 · JFK 01:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.