简体   繁体   English

Celery + RabbitMQ结果后端+ WebSockets?

[英]Celery + RabbitMQ Results Backend + WebSockets?

I have a Flask web app and there are some tasks that are long running (> 1 minute). 我有一个Flask Web应用程序,有一些任务长期运行(> 1分钟)。 I would like to offload these from flask (blocking web server, etc) and move them into external tasks. 我想从烧瓶中卸载这些(阻止Web服务器等)并将它们移动到外部任务中。 I got Celery setup and am able to run these tasks from Flask. 我得到了Celery设置,能够从Flask运行这些任务。 Good. 好。

To notify users when the task is done and return the results, I wanted to use web sockets. 要在任务完成时通知用户并返回结果,我想使用Web套接字。 This is where it gets hard. 这是它变得艰难的地方。

I have a gevent websocket server running that my client connects to. 我有一个运行我的客户端连接的gevent websocket服务器。 When I launch the celery task I pass in the session ID of the client so that when I get my results I know who they belong to. 当我启动芹菜任务时,我会传递客户端的会话ID,这样当我得到我的结果时,我知道他们属于谁。

Now I need the websocket server to know when these tasks are done, look at the results, and send them to the appropriate connection. 现在我需要websocket服务器知道这些任务何时完成,查看结果,并将它们发送到适当的连接。

How? 怎么样? This is where I get completely lost. 这是我完全迷失的地方。 I can listen for celery events with celery.events.EventListener.capture , but this blocks my websocket server so I can't have multiple connections! 我可以用celery.events.EventListener.capture监听celery事件,但这会阻止我的websocket服务器,所以我不能有多个连接!

How can I listen for celery events without blocking? 如何在不阻塞的情况下收听芹菜事件?

One idea is to make the request over the web socket and then launch the celery task from my websocket server. 一个想法是通过Web套接字发出请求,然后从我的websocket服务器启动celery任务。 That would solve the notification issue (just used celery events), but I'm still curious about my original question where the request was made to my standard flask server. 这将解决通知问题(仅使用芹菜事件),但我仍然对我的原始问题感到好奇,其中请求是我的标准烧瓶服务器。

You need your flask app not to block on the capture call. 你需要你的烧瓶应用程序不要阻止捕获呼叫。 One solution is to run flask with gevent or eventlet so that your process will handle request asynchronously and blocking calls will not block the process. 一种解决方案是使用gevent或eventlet运行flask,以便您的进程将异步处理请求,阻塞调用不会阻止进程。

There are tons of examples about this, to name one: https://gist.github.com/lrvick/1185629 有很多例子,请注意: https//gist.github.com/lrvick/1185629

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM