在下面的代码中,当我执行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
答案 0 :(得分:2)
你应该尝试这样的事情: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))