通过线程(或替代方式)在一个应用程序中使用多个反应器(主循环)
我有一个应用程序的想法,我想开发一个应用程序,以进一步了解 Twisted 和WebSocket。我正在考虑将之前编写的 IRC Bot 集成到 Web 应用程序中。据我所知,我需要三个反应器才能使其工作:
- 主反应器:Web 服务器(HTTP)。这将是您的普通twisted.web 应用程序。当您访问它时,您可以将 IRC 服务器/通道发布到连接。然后,Web 服务器将与不同线程中的不同反应器进行通信,即...
- 辅助反应器:IRC Bot。这将是一个通过 Twisted IRC 客户端协议运行的 IRC 机器人。它将加入一个通道,每当有人说话时,它都会获取该数据并将其推送到另一个线程上的另一个反应器,即...
- 第三级反应器:WebSocket 服务器(WS) :由于 WebSocket 不使用常规的 HTTP 协议,因此它们需要自己的服务器(或者看起来是这样,查看诸如 this。当IRC机器人收到消息时,它告诉WebSocket服务器将该消息推送到连接的客户端。
在我看来 中无法完成的事情。
,这似乎是有可能的。是否有人有在单独线程中运行的多个反应器的示例,或者这是我想象的在 Twisted 的当前版本 可以(或应该)进行架构更改以最小化反应堆数量等?
感谢您的帮助。
I've got an idea for an app I'd like to work on to learn a bit more about Twisted and WebSockets. I was thinking of integrating a previously written IRC Bot into a web application. As far as I can see it, I would need three reactors to make it work:
- Primary Reactor: Web Server (HTTP). This would be your average twisted.web application. When you access it, you can POST an IRC server/channel to connection. The web server would then talk to a different reactor in a different thread, which is...
- Secondary Reactor: IRC Bot. This would be an IRC bot running via the Twisted IRC client protocol. It would join a channel, and whenever something was said, it would take that data and push it to yet another reactor, on yet another thread, which is...
- Tertiary Reactor: WebSocket Server (WS): Since WebSockets don't use the regular HTTP Protocol, they need their own server (or so it seems, looking at examples such as this. When the IRC bot receives a message, it tells the WebSocket Server to push that message to connected clients.
In my mind, this makes sense. It seems like it would be possible. Does anyone have any examples of multiple reactors running in separate threads, or is this something I've imagined that can't be done in the current incarnation of Twisted.
Are there any architecture changes that can (or should) be made to minimize the reactor count, etc?
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
幸运的是,很容易减少反应器的数量,特别是减少到 1:
在任何给定的 Twisted 进程中,单个线程中只能有一个反应器。如果你试图拥有更多,那么什么都不会起作用。
实际上,反应器的全部意义在于促进将多个事件源组合到单个线程中。如果您想使用 3 个不同的协议监听 3 个不同的端口,您的应用程序可能如下所示:
当然,您实际上可能不会自己直接调用
listenTCP
,因为您可能想使用如果您使用
对象,可以通过twistd
,则来自twisted.application.internet
的 Service.tac
文件或扭曲插件。如果
twistd
为您做这件事,您就不需要自己调用reactor.run()
。我的观点是,无论通过什么方式,你都可以向反应堆加载你期望它做出反应的所有事件——监听服务器、客户端连接、定时事件——并且它会在每一个事件发生时做出反应。 (因此,“reactor”。)有关
FirstProtocolFactory
、SecondProtocolFactory
和ThirdProtocolFactory
的具体值,请参阅 pyfunc 答案中的链接。Lucky for you, it is easy to reduce the number of reactors, specifically, to 1:
You can only ever have a single reactor, in a single thread, in any given Twisted process. If you try to have more, nothing will work.
The whole point of a reactor, actually, is to facilitate having multiple sources of events combined into a single thread. If you want to listen on 3 different ports with 3 different protocols, your application might look like this:
Of course, you may not actually be calling
listenTCP
directly yourself, as you probably want to useService
objects fromtwisted.application.internet
if you are usingtwistd
, either via a.tac
file or atwistd
plugin. And you won't need to callreactor.run()
yourself, iftwistd
is doing it for you. My point here is that, via whatever means, you load up the reactor with all the events you expect it to react to - listening servers, client connections, timed events - and it will react to each one as it occurs. (Hence, "reactor".)For the specific values of what
FirstProtocolFactory
,SecondProtocolFactory
, andThirdProtocolFactory
should be, see the links in pyfunc's answer.您需要的是一个多服务多协议应用程序。这就是 Twisted 真正闪光的地方。
因此,您的应用程序应该启动 Web 服务、IRC Bot 服务和 WebSocket 服务器。
查看 IRC 机器人实现和扭曲 IRC 协议支持:
以及 websocket 和twisted
What you need, is a multi-service multi-protocol application. This is where Twisted really shines.
So your application should start a web service, IRC Bot service and WebSocket server.
Check out the IRC bot implementation and twisted IRC protocol support:
and for websocket and twisted