Python 用于芹菜任务监控和发布到React应用程序的WAMP组件

Python 用于芹菜任务监控和发布到React应用程序的WAMP组件,python,celery,publish-subscribe,crossbar,wamp-protocol,Python,Celery,Publish Subscribe,Crossbar,Wamp Protocol,我有一个React应用程序,通过Klein发布并异步运行芹菜作业(通过RabbitMQ)。我希望通过WAMP发布/订阅(我使用Crossbar作为路由器)在一些表中显示状态更新的所有任务。My React table组件获取初始数据并订阅如下更改: class Table extends React.Component { componentWillMount(){ this.props.session //Autobahn session variable passed down

我有一个React应用程序,通过Klein发布并异步运行芹菜作业(通过RabbitMQ)。我希望通过WAMP发布/订阅(我使用Crossbar作为路由器)在一些表中显示状态更新的所有任务。My React table组件获取初始数据并订阅如下更改:

class Table extends React.Component {
  componentWillMount(){
    this.props.session //Autobahn session variable passed down as prop
      .subscribe("celery.update.task", (args,kwargs,details)=>{update state})
  }
  componentDidMount(){
    //Gets all tasks in db (SQLite)
    this.props.session.call("celery.all.tasks", (a,k,d)=>{set initial state}
  }
  render() {
    //render table with data from state
  }
我目前正在尝试让后端的WAMP组件(使用AutobahnPython)在芹菜事件上发布“celery.update.task”。这个应用程序将面向internet,所以我想使用安全websockets(wss)并遵循运行WAMP组件的教程。我当前的问题是,WAMP组件并不异步发布(一旦python进程结束,所有发布都会被发送):

上面的代码能够监视所有事件,但在python进程结束之前,不会将发布方法推送到订阅者。我希望WAMP组件在每个芹菜事件(接收、启动、成功、失败)上发布到“celery.task.update”,以便实时更新表组件

有没有办法让这一切顺利进行?我试过了,但没有成功。我试了又试

import threading, time, ast, six
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunnner
from twisted.internet import defer
from twisted.internet.defer import inlineCallbacks, returnValue
from twisted.internet._sslverify import OpenSSLCertificateAuthorities
from twisted.internet.ssl import CertificateOptions
from OpenSSL import crypto

class MonitorThread(ApplicationSession):
  @inlineCallbacks
  def onJoin(self, details):
    from app_queue import celery #Celery instance
    self.celery_app = celery
    self.interval = 1
    self.state = self.celery_app.events.State()
    self.thread = threading.Thread(target=self.run, args=())
    self.thread.daemon = True
    self.thread.start()
    yield 

  @inlineCallbacks
  #there are analogous callbacks for "received", "started" and "failed"
  def handle_task_success(self, event):
    ...
    res = yield self.publish("celery.task.update", "some_task_update") 
    resultValue(res)

  def run(self): 
    while True:
      try:
        with self.celery_app.connection() as connection: 
          recv = self.select_app.events.Receiver(connection, handlers={
            "task-succeeded": self.handle_task_success
          })
          recv.capture(limit=None, timeout=None, wakeup=True)
      except (KeyboardInterrupt, SystemExit):
        raise
      except Exception:
        pass
      time.sleep(self.interval)

if __name__ == "__main__":
  cert = crypto.load_certificate(crypto.FILETYPE_PEM,six.u(open('.crossbar/example.cert.pem', "r").read()))
  options = CertificateOptions(trustRoot=OpenSSLCertificateAuthorities([cert]))
  runner = ApplicationRunner(url=u"wss://127.0.0.1:443/ws", realm=u"realm1", ssl=options)
  runner.run(MonitorThread)