简体   繁体   中英

Celery Inspect task from python

from celery import Celery
from celery.worker.control import inspect_command
app = Celery('tasks', broker='pyamqp://guest@localhost//')

@app.task
def add(x, y):
    return x + y


@inspect_command
def current_prefetch_count(state):
    return {'prefetch_count': state.consumer.qos.value}

I tried running the code with Celery, but it gave me an error:

File"c:\python27\lib\site-packages\kombu\utils\imports.py",line56,insymbo
_by_name

typeError: inspect_command() takes exactly 0 arguments (1 given)

Also, I wanted to customize inspect_command to run this sysfile.py

#!/usr/bin/env python
import platform
import celery
import os
import psutil
import json




def Speed_Test():
    Speed_list = (os.popen("speedtest-cli --share --simple").read()).split("\n")
    result = [Speed_list[-2].split(":")[-1]]
    result.append(Speed_list[-3].split(":")[-1])
    return result

def Sys_Info():

    inner_Dict = {}
    inner_Dict["CPU Model"]=platform.processor()
    inner_Dict["No of CPU"]=psutil.cpu_count()
    inner_Dict["Disk info"]=psutil.disk_usage('/')
    inner_Dict["celery"] = (celery.__version__)
    inner_Dict["Upload"] = Speed_Test()[0]
    inner_Dict["Download"] = Speed_Test()[1]
    return json.dumps(inner_Dict)
x = Sys_Info()
print x

The inspect_command decorator optionally takes a series of kwargs . So you have to call it:

@inspect_command()
def current_prefetch_count(state):
    return {'prefetch_count': state.consumer.qos.value}

For instance if you want the command to have an other name by which it can be called, you can use alias :

@inspect_command(alias='foo')
def current_prefetch_count(state):
    return {'prefetch_count': state.consumer.qos.value}

You can discover all of this by reading the code of celery.worker.control .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM