在 Twisted 中实现 Direct Connect 客户端的正确方法?

发布于 2024-08-13 21:30:31 字数 357 浏览 4 评论 0原文

我正在为 Direct Connect P2P 网络编写一个 Python 客户端。本质上,它的工作原理是连接到中央服务器,并响应正在搜索文件的其他用户。

有时,另一个客户会要求我们连接到他们,他们可能会开始从我们这里下载文件。这是与其他客户端的直接连接,不通过中央服务器。

处理与其他客户端的这些连接的最佳方法是什么?我目前正在使用一个 Twisted 反应器连接到服务器,但是是否最好拥有多个反应器,每个客户端一个,每个反应器在不同的线程中运行?或者有一个完全独立的 Python 脚本来执行与客户端的连接会更好吗?

如果还有其他我不知道的解决方案,我很想听听。我是 Twisted 编程新手,因此我愿意接受建议和其他资源。

谢谢!

I'm working on writing a Python client for Direct Connect P2P networks. Essentially, it works by connecting to a central server, and responding to other users who are searching for files.

Occasionally, another client will ask us to connect to them, and they might begin downloading a file from us. This is a direct connection to the other client, and doesn't go through the central server.

What is the best way to handle these connections to other clients? I'm currently using one Twisted reactor to connect to the server, but is it better have multiple reactors, one per client, with each one running in a different thread? Or would it be better to have a completely separate Python script that performs the connection to the client?

If there's some other solution that I don't know about, I'd love to hear it. I'm new to programming with Twisted, so I'm open to suggestions and other resources.

Thanks!

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

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

发布评论

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

评论(1

鹊巢 2024-08-20 21:30:31

在不了解协议的所有细节的情况下,我仍然建议使用单个反应器——反应器的扩展性相当(尤其是高级的,例如PollReactor),这样你就可以将避免与线程相关的开销(毕竟,这就是 Twisted 和其他异步系统通过避免此类开销来获得基本性能提升的方式)。实际上,Twisted 中的线程主要在您需要连接到某个库(该库的函数可能会阻塞您)时非常有用。

Without knowing all the details of the protocol, I would still recommend using a single reactor -- a reactor scales quite well (especially advanced ones such as PollReactor) and this way you will avoid the overhead connected with threads (that's how Twisted and other async systems get their fundamental performance boost, after all -- by avoiding such overhead). In practice, threads in Twisted are useful mainly when you need to interface to a library whose functions could block on you.

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