推荐学习书目
› 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 · Sep 25, 2019 · 2644 views
    This topic created in 2564 days ago, the information mentioned may be changed or developed.

    有个项目需要用到后台任务,选了 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 实现不了别的库也行。

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   933 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:01 · PVG 03:01 · LAX 12:01 · JFK 15:01
    ♥ Do have faith in what you're doing.