Hacer que el apio vuelva a ser un futuro

I am using the tornado web framework. Is it possible to make a celery task returning an object of the Futuro class, in order to use it in an @gen.coroutine decorated handler?

Lo que intento hacer es lo siguiente:

    class TornadoRequestHandler(BaseHandler):

         @gen.coroutine
         def get(self):
              result = yield celery_task.apply_aync()
              self.write(result)
              self.finish()

He visto tornado-celery, but that isn't exactly what i am trying to achieve.

preguntado el 24 de mayo de 14 a las 16:05

1 Respuestas

As far as I know, the only way to do this is via tornado-celery, which will let you do this:

class TornadoRequestHandler(BaseHandler):

     @gen.coroutine
     def get(self):
          result = yield gen.Task(celery_task.apply_aync)
          self.write(result)
          self.finish()

The reason is that the behavior you want when using gen.coroutine relies on all the async methods it calls to take a callback kwarg, which gets called when the method completes. celery_task.apply_async no toma un callback kwarg, so gen.coroutine can't be used directly. It looks like tornado-celery works by exploiting the fact that apply_async does take an **options parameter, which can be any arbitrary kwargs. This means that apply_async actually will accept a callback kwarg, but just ignore it. tornado-celery takes advantage of this by overriding the class in Celery responsible for publishing tasks, and changes the publishing process to publish your task, and then consume from the result queue that gets published to when the tasks completes. The consuming code executes the function provided by the normally ignored callback kwarg, which it pulls out **options.

I'm not sure how clear that explanation, was, but the tl;dr version is tornado-celery provides the simplest way of getting as close possible to the behavior you want.

contestado el 25 de mayo de 14 a las 16:05

HI dano! Thank you for your answer. I think so, too :), and tornado-celery is a nice library, but now i solved it by running the celery tasks in an executor, using a caller function that waits for the task to be finished. It s little code and it wokrs with tornado standard tools. - eatdas

the concept of result for gen and celery's apply_async are quite different, one is future in ioloop, another one is future in task queue. The later one should be gotten be result.get() while the prior one is by yield. However, the tornado-celery mixed them altogether. - jim horng

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.