在后台使用 Twisted 代理 TCP 套接字的一般方法?

发布于 2024-10-20 14:30:09 字数 857 浏览 2 评论 0原文

我正在寻找一些关于如何解决我认为 Twisted 非常适合的问题的一般信息。 (我是 Twisted 的新手,但不是 Python)

我有一个家庭自动化控制器,可以支持单个 TCP 套接字连接,发送和接收二进制数据。我想使用 XMPP 作为套接字的桥梁,以便用户可以发送命令和接收事件。

我得到了一个与 Twisted 一起使用的基本套接字连接,它能够从 O'Reilly 书中的示例之一发送和接收命令。我还有一个使用 SleekXMPP 库编写的完全可用的 Python XMPP 机器人,我对此很满意。我只是不确定如何将这些结合在一起。

基本场景是:

  1. 用户向 XMPP 机器人发送消息,XMPP 机器人确定要发送到套接字的命令
  2. ASCII Socket 命令转换为二进制并发送到套接字
  3. Socket 接收命令并发送二进制响应
  4. 二进制响应转换为 ASCII
  5. XMPP 机器人发送回响应给用户。
  6. 网络事件(独立于用户操作)也可以由网络套接字接收,并应该发送给用户。

#6 提出了挑战,否则我只需在需要写入内容时按需打开/关闭套接字。

我在 Twisted 中遇到困难的部分是使这两个事件循环进行通信的最佳方法。我看过很多关于使用队列、延迟、线程、选择等的信息。我有一种感觉,如果我学会正确使用该工具,Twisted 可以处理大部分复杂性。

如果有人能给我指明正确的方向,我就会带球跑。正如我提到的,我对我的 XMPP 机器人很满意,并且我想使用现有的代码。我认为我的问题现在归结为在后台创建套接字,然后在前台从该套接字发送和接收数据。

顺便说一句,我很高兴在代码运行后将其分享回来,这样其他人就可以从我所寻求的帮助中受益。

——斯科特

I'm looking for some general information on how I should approach a problem that I think Twisted is a great fit for. (I'm new to Twisted but not Python)

I have a home automation controller that can support a single TCP socket connection, sending and receiving binary data. I'd like to use XMPP as a bridge to the socket so a user can send commands and receive events.

I got a rudimentary socket connection working with Twisted that was able to send and receive commands from one of the examples in the O'Reilly book. I also have a fully working Python XMPP bot written with the SleekXMPP library that I'm happy with. I'm just not sure how to bring these together.

The basic scenario is:

  1. User sends message to XMPP bot, which figures out what command to send to the socket
  2. ASCII Socket command is converted to binary and sent to socket
  3. Socket receives command and sends binary response
  4. Binary response converted to ASCII
  5. XMPP bot sends response back to user.
  6. Network events (independent from user action) can also be received by network socket and should be sent to user

It's #6 that is presenting the challenge, otherwise I'd just open/close the socket on demand when in need to write something.

The part that I'm having trouble wrapping my head around with Twisted is the best approach to make these two event loops communicate. I've seen lots of info on using Queues, deferred, threads, select, etc. I have a feeling that Twisted can handle much of the complexity if I just learn to use the tool properly.

If someone can point me in the right direction, I'll take the ball and run with it. As I mentioned, I'm happy with my XMPP bot and I'd like to use the existing code. I think my problem now comes down to creating the socket in the background, then sending and receiving data from that socket in the foreground.

By the way, I'm very happy to share back my code once it's working so someone else can benefit from the help I'm asking for.

-- Scott

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

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

发布评论

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

评论(1

下壹個目標 2024-10-27 14:30:09

非阻塞 IO 引擎的问题之一是它几乎是全有或全无。一旦引入阻塞代码,您很快就会失去事件驱动异步方法的大部分好处。只要有可能(根据经验),最好让整个应用程序在同一个反应器上运行。

在我看来,你有两个选择:

  1. Twisted 不是线程安全的。也就是说,您可以使用 deferToThread 等机制和 callFromThread 与其他线程交互。对于应用程序设计来说,这是迄今为止最令人困惑且不必要的复杂方法。如果你刚接触 Twisted,这会特别痛苦。
  2. 使用twisted.words.protocols.jabber,并使用twistedreactor以非阻塞方式实现您的XMPP内容。这样它就会愉快地与所有其他扭曲的代码一起存在。并允许您在协议之间干净地交互。它将减少代码量,并实现易于扩展、维护和测试的健壮实现。

One of the problems with a non-blocking IO engine is that its pretty much all-or-nothing. As soon as you introduce blocking code, you can quickly lose most of the benefits of the event-driven asynch approach. Wherever possible (as a rule of thumb), its best to have the entire app running off the same reactor.

As i see it, you have two options:

  1. Twisted is not thread safe. That said, you can use mechanisms like deferToThread and callFromThread to interact with other threads. This is by far the most confusing and needlessly complex approach for your application design. It's particularly painful if you're new to twisted.
  2. Use twisted.words.protocols.jabber, and implement your XMPP stuff in a non-blocking manner using the twisted reactor. That way it will happily exist alongside all your other twisted code. and allow you to cleanly interact between protocols. It will result in less code, and a robust implementation that is easy to extend, maintain, and test.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文