celery - django 获取当前 django 用户的所有正在运行/挂起的 celery 任务

标签 celery django-celery

是否有任何 pythonic 方法来获取当前登录的 django 用户的所有正在运行/挂起的 celery 任务?我正在尝试的伪代码:

    @celery.task
    def process_task(user, task_to_do):
        #get all running or pending(queued) task for current user
        user_tasks = user.get_task(status=PENDING or status=STARTED)
        if not user_task:
            #allow user to schedule additional task
            process....
        else:
            return "Your previous tasks is already running"

最佳答案

这通常是一项棘手的任务。

First you need to implement inspecting of workers

inspector = app.control.inspect()
scheduled = inspector.scheduled()
reserved = inspector.reserved()
active = inspector.active()

Celery 将从您的经纪人处获取它们。要点是 - 代理不存储有关用户的信息,因此您需要将 user 添加到任务 kwargs 中。

user_task.delay(user=user)

您将能够通过结果中的 kwarg user 过滤这些函数的结果: [{'worker1.example.com': [{'eta': '2010-06-07 09:07:52', '优先级': 0, '要求': { '名称': '任务.usertask', 'id': '1a7980ea-8b19-413e-91d2-0b74f3844c4d', 'args': '[]', 'kwargs': '{'用户':'7'}'}}, ...

The problem here - it will be slow.

关于celery - django 获取当前 django 用户的所有正在运行/挂起的 celery 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36784791/

相关文章:

python - 如何使用 Prometheus 监控 Celery 任务完成情况

python - 如何使用同一个 worker 重试 celery ?

python - Celery 3.1 - 异步执行任务链

django - 从一条线上处决两名 celery worker

python - 通过 Django-celery 安排数千个一次性(非经常性)任务以近乎同时执行

python - 用户 rabbitmq 和 celery 的访问被拒绝

flask - 带create_app,SQLAlchemy和Celery的 flask

django - 无法在 django 1.5.4 中迁移 djcelery

django - Amazon SQS 和 celery 事件(不可序列化的 JSON)

mysql - Django OperationalError 2019 无法初始化字符集utf8mb4