在下面的代码中,当我执行message_response.get()时,它将使这个特定的代码同步。现在有没有一种方法可以让我做这件事?只需将代码推送到代理即可。一旦芹菜工人完成了任务,我可以把结果写回客户端吗?
import tornado.websocket
from celery_main import do_something_celery_task
class HomePageRequestHandler(tornado.websocket.WebSocketHandler):
def on_message(self, message):
message_response = do_something_celery_task.apply_async((message,))
# How can this be a non blocking call?
self.write_message(message_response.get())
def open(self):
pass
您应该尝试以下操作:https://github.com/mher/tornado-celery你会有这样的代码,但我不运行它。
from tornado.websocket import WebSocketHandler
class WebSocketBase(WebSocketHandler):
@gen.coroutine
def on_message(self, message):
response = yield gen.Task(tasks.sleep.apply_async, args=[3])
self.write_message(str(response.result))