Python (Twisted) - 从 fifo 读取并将读取的数据发送到多个协议

发布于 2024-08-25 13:41:41 字数 308 浏览 11 评论 0原文

我正在尝试编写某种多协议机器人(jabber/irc),它可以从 fifo 文件(主要是一个衬垫)读取消息,然后将它们发送到 irc 通道和 jabber 联系人。到目前为止,我成功创建了两个工厂来连接 jabber 和 irc,并且它们似乎正在工作。

但是,我在读取 fifo 文件时遇到问题 - 我不知道如何在反应器循环之外的循环中读取它(打开文件、读取行、关闭文件、跳转到打开文件等)以获取我的数据需要发送,然后将该数据发送到反应器循环以在两种协议中发送。我一直在寻找有关如何以最佳方式做到这一点的信息,但我完全迷失在黑暗中。任何建议/帮助将不胜感激。

提前致谢!

Im trying to write some kind of multi protocol bot (jabber/irc) that would read messages from fifo file (one liners mostly) and then send them to irc channel and jabber contacts. So far, I managed to create two factories to connect to jabber and irc, and they seem to be working.

However, I've problem with reading the fifo file - I have no idea how to read it in a loop (open file, read line, close file, jump to open file and so on) outside of reactor loop to get the data I need to send, and then get that data to reactor loop for sending in both protocols. I've been looking for information on how to do it in best way, but Im totally lost in the dark. Any suggestion/help would be highly appreciated.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简美 2024-09-01 13:41:41

您可以像使用套接字一样在文件描述符上读/写而不阻塞反应器,顺便问一下,套接字不使用文件描述符吗?

在您的情况下,创建一个实现twisted.internet.interfaces.IReadDescriptor的类,并使用twisted.internet.interfaces.IReactorFDSet.addReader添加到反应器。有关 IReadDescriptor 实现的示例,请查看 twisted.internet.tcp.Connection

我不能更具体,因为我从来没有自己做过,但我希望这可以成为一个起点。

You can read/write on a file descriptor without blocking the reactor as you do with sockets, by the way doesn't sockets use file descriptors?

In your case create a class that implements twisted.internet.interfaces.IReadDescriptor and add to reactor using twisted.internet.interfaces.IReactorFDSet.addReader. For an example of IReadDescriptor implementation look at twisted.internet.tcp.Connection.

I cannot be more specific because i never did by my self, but i hope this could be a start point.

就是爱搞怪 2024-09-01 13:41:41

fifo 是问题所在。改为从套接字读取。这将更好地适应 Twisted 事件驱动模型。试图在反应堆控制之外做事情通常是错误的做法。

---- 根据反馈进行更新,fifo 是外部约束,无法避免 ----

好吧,核心问题是你不能在 Twisted 应用程序的主(也是唯一)线程中编写代码来进行阻塞读取调用 fifo。如果没有任何内容可读取,这将导致整个应用程序停止运行。因此,您要么考虑异步读取 fifo,创建一个单独的线程来读取它,要么将应用程序一分为二。

最后一个选项是最简单的 - 修改 Twisted 应用程序,使其侦听套接字并编写一个单独的小“转发器”应用程序,该应用程序在一个简单的循环中运行,读取 fifo 并将其听到的所有内容写入套接字。

The fifo is the problem. Read from a socket instead. This will fit info the Twisted event-driven model much better. Trying to do things outside the control of the reactor is usually the wrong approach.

---- update based on feedback that the fifo is an external constraint, not avoidable ----

OK, the central issue is that you can not write code in the main (and only) thread of your Twisted app that makes blocking read calls to a fifo. That will cause the whole app to stall if there is nothing to read. So you're either looking at reading the fifo asynchronously, creating a separate thread to read it, or splitting the app in two.

The last option is the simplest - modify the Twisted app so that it listens on a socket and write a separate little "forwarder" app that runs in a simple loop, reading the fifo and writing everything it hears to the socket.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文