在Celery中实现Twisted样式的本地多延迟回调

时间:2013-12-09 16:26:50

标签: python twisted celery

我很擅长使用Celery,并想知道如何在Celery中实现TWSITED类型的多个延迟回调

MY TWISTED CODE使用透视经纪人,如下所示。我有一个Handler(服务器)处理一些事件并返回结果。 Dispatcher(Client)使用延迟回调打印返回的结果。

Handler.py(服务器)

from twisted.application import service, internet
from twisted.internet import reactor, task
from twisted.spread import pb
from Dispatcher import Event
from Dispatcher import CopyEvent

class ReceiverEvent(pb.RemoteCopy, Event):
    pass
pb.setUnjellyableForClass(CopyEvent, ReceiverEvent)


class Handler(pb.Root):

def remote_eventEnqueue(self, pond):
    d = task.deferLater(reactor,5,handle_event,sender=self)
    return d

def handle_event(sender):
    print "Do Something"
    return "did something"

if __name__ == '__main__':
    h=Handler()
    reactor.listenTCP(8739, pb.PBServerFactory(h))
    reactor.run()

现在Dispatcher.py(客户端)

from twisted.spread import pb, jelly
from twisted.python import log
from twisted.internet import reactor
from Event import Event

class CopyEvent(Event, pb.Copyable):
    pass

class Dispatcher:
    def __init__(self, event):
        self.event = event

    def dispatch_event(self, remote):
        d = remote.callRemote("eventEnqueue", self.event)   
        d.addCallback(self.printMessage)

    def printMessage(self, text):
        print text

def main():
    from Handler import CopyEvent
    event = CopyEvent()
    d = Dispatcher(event)
    factory = pb.PBClientFactory()
    reactor.connectTCP("localhost", 8739, factory)
    deferred = factory.getRootObject()
    deferred.addCallback(d.dispatch_event)
    reactor.run()

if __name__ == '__main__':
    main()

我尝试在Celery中实现这一点。

Handler.py(服务器)

from celery import Celery

app=Celery('tasks',backend='amqp',broker='amqp://guest@localhost//')

@app.task

def handle_event():
     print "Do Something"
     return "did something"

Dispatcher.py(客户端)

from Handler import handle_event
from datetime import datetime

def print_message(text):
    print text


t=handle_event.apply_async(countdown=10,link=print_message.s('Done'))  ##HOWTO?

我的确切问题是如何在Celery中的print_message等本地函数上实现延迟回调TWISTED样式。当handle_Event方法完成后,它返回我希望有另一个回调方法(print_message)的结果,这是LOCAL

在Celery中执行此操作的任何其他可能的设计工作流程?

谢谢

JR

2 个答案:

答案 0 :(得分:2)

好的,所以终于明白了。在Twlery样式中直接在Celery客户端中添加回调是不可能的。但Celery支持任务监视功能,使客户端能够监视不同类型的工作事件并在其上添加回调。

一个简单的任务监视器(Task_Monitor.py)看起来像这样。 (详细信息可以在Celery实际处理文档中找到http://docs.celeryproject.org/en/latest/userguide/monitoring.html#real-time-processing

Task_Monitor.py

from celery import Celery

def task_monitor(app):
    state = app.events.State()

    def announce_completed_tasks(event):
        state.event(event)
        task = state.tasks.get(event['uuid'])

        print('TASK SUCCEEDED: %s[%s] %s' % (task.name, task.uuid, task.info(), ))

    with app.connection() as connection:
        recv = app.events.Receiver(connection, handlers={'task-succeeded': announce_completed_tasks})
        recv.capture(limit=None, timeout=None, wakeup=True)

if __name__ == '__main__':
    app = Celery(broker='amqp://guest@REMOTEHOST//')
    task_monitor(app)

Task_Monitor.py必须作为单独的进程(客户端)运行。除了Celery应用程序(服务器端)需要使用

进行配置
app.conf.CELERY_SEND_EVENTS = TRUE

或在运行celery时使用-E选项

以便它发送事件以便监视工作人员。

答案 1 :(得分:0)

我建议使用链或Celery Canvas docs的其中一种类似机制。

从文档中获取的示例:

>>> from celery import chain
>>> from proj.tasks import add, mul

# (4 + 4) * 8 * 10
>>> res = chain(add.s(4, 4), mul.s(8), mul.s(10))
proj.tasks.add(4, 4) | proj.tasks.mul(8) | proj.tasks.mul(10)
>>> res.apply_async()