如何将类成员方法作为celery任务? - V2EX
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
kenneth
V2EX    Python

如何将类成员方法作为celery任务?

  •  
  •   kenneth 2013-07-14 21:42:29 +08:00 4475 次点击
    这是一个创建于 4548 天前的主题,其中的信息可能已经有所发展或是发生改变。
    想将类成员方法作为celery task,但是后端没有收到消息。
    求方法,我试了几次不行,不知道哪里错了
    19 条回复    1970-01-01 08:00:00 +08:00
    gfreezy
        1
    gfreezy  
       2013-07-14 22:09:14 +08:00
    新写一个function作为celery_task,里面调用 类成员方法
    yishenggudou
        2
    yishenggudou  
       2013-07-15 10:38:40 +08:00   1
    celery 太折腾 不推荐
    zhenyi2697
        3
    zhenyi2697  
       2013-07-15 14:27:27 +08:00
    @yishenggudou 那消息队列有什么推荐的呢?
    kenneth
        5
    kenneth  
    OP
       2013-07-15 15:09:29 +08:00
    @ritksm 这篇文章我看到了,但是我没能成功,broker还是没有收到消息。
    ritksm
        6
    ritksm  
       2013-07-15 15:20:14 +08:00
    @kenneth 检查配置、检查log、检查调用的shell命令、贴出代码。要不然别人怎么发现你的问题?
    kenneth
        7
    kenneth  
    OP
       2013-07-15 15:23:46 +08:00
    class A:
    def __init__(self, feed):

    @celery.task
    def process(self):
    return

    a = A()
    a.process.delay(feed)

    @ritksm 但是不行。高手们帮看下。
    wenbinwu
        8
    wenbinwu  
       2013-07-15 15:26:44 +08:00
    @kenneth 你的代码貌似没参考@ritksm 给你的文档啊
    kenneth
        9
    kenneth  
    OP
       2013-07-15 15:29:04 +08:00
    @wenbinwu 已经给出了关键信息了,就是调用类方法作为任务,这就是核心问题。
    wenbinwu
        10
    wenbinwu  
       2013-07-15 15:30:22 +08:00
    @kenneth 你再读一下那个连接。。。
    kenneth
        11
    kenneth  
    OP
       2013-07-15 15:32:21 +08:00
    @wenbinwu 读过了,还是不知道问题在哪里?
    wenbinwu
        12
    wenbinwu  
       2013-07-15 15:33:57 +08:00
    @kenneth 你按照第一个例子改一下,别用 ` @celery.task `
    kenneth
        13
    kenneth  
    OP
       2013-07-15 15:39:38 +08:00
    @wenbinwu

    Traceback (most recent call last):
    File "/usr/local/lib/python2.7/dist-packages/celery/task/trace.py", line 233, in trace_task
    R = retval = fun(*args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/celery/task/trace.py", line 420, in __protected_call__
    return self.run(*args, **kwargs)
    None: process() takes exactly 1 argument (0 given)
    ritksm
        14
    ritksm  
       2013-07-15 15:45:33 +08:00
    参见以下代码:
    from celery import Celery
    from celery.contrib.methods import task_method

    celery = Celery('xxxxx', broker='xxxxxx')

    class A(object):

    @celery.task(filter=task_method)
    def process(self):
    print('ok')
    return
    kenneth
        15
    kenneth  
    OP
       2013-07-15 15:51:12 +08:00
    @ritksm 试了,这种方法就根本收不到消息,跑都不跑。
    ritksm
        16
    ritksm  
       2013-07-15 15:53:21 +08:00
    如何不跑了。。。我咋就可以出结果。。。难道要我截图出来么。。。错误信息?shell是如何调用celery的。。。
    kenneth
        17
    kenneth  
    OP
       2013-07-15 15:55:24 +08:00
    @ritksm 没有错误,只是worker收不到job。我们加下Gtalk交流吧:[email protected]
    Archangel_SDY
        18
    Archangel_SDY  
       2013-07-15 16:51:22 +08:00
    看看broker的log呢?

    上次被RabbitMQ的1G磁盘剩余空间要求坑了整整一晚上...
    zhenyi2697
        19
    zhenyi2697  
       2013-07-15 20:25:07 +08:00
    @kenneth 貌似调用celery需要task所在的module是一个celerymodule

    也就是说,你需要先运行 celery = Celery('xxxxx', broker='xxxxxx') 创建一个celery的app,然后再写你的task。
    另外,你需要运行一个celery的worker,参考 celery -A app_name worker --loglevel=info
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2687 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 31ms UTC 01:56 PVG 09:56 LAX 17:56 JFK 20:56
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86