使用 Twisted Python 的多个通信渠道

发布于 2024-09-27 22:58:04 字数 483 浏览 2 评论 0原文

我目前正在研究 Twisted 框架作为实现基于网络的备份应用程序的一种方式,我想实现一些我在网上找不到任何示例的东西。

我计划使用 Perspective Broker 来实现该系统,但我还需要一种将二进制文件从客户端传输到服务器的方法。我希望能够调用 PB 上的方法,然后使用某种 UID 通过单独的数据通道发送文件。

拥有这两个单独的通信通道的原因是我想让客户端成为多线程的(一个线程扫描目录树,而另一个线程将更改的文件传输到服务器)。

Twisted 可以做到这一点吗?我读过,在反应器上有多个线程调用方法是个坏消息,那么这种架构注定会失败吗?

我将不胜感激任何正确方向的指示,正如我提到的,我仍在研究可能性 - 但我计划在这个项目中使用 Django,所以 Python 是必须的。

I am currently researching the Twisted framework as a way of implementing a network-based backup application, and I would like to achieve something that I cannot find any examples of on the net.

I plan to implement the system using the Perspective Broker, but I will also need a way of transferring binary files from the client to the server. I would like to be able to call a method on the PB, and then use some sort of UID to send the file over a separate data channel.

The reason for having these two separate communication channels is down to the fact that I would like to make the client multi-threaded (one thread scanning the directory tree, while another thread transfers the changed files to the server).

Is this possible with Twisted? I have read that having multiple threads calling methods on a reactor is bad news, so is this architecture doomed to failure?

I would appreciate any pointers in the right direction, as I mentioned I am still researching the possibilities - but I plan to use Django for this project, so Python is a must.

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

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

发布评论

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

评论(1

岁月染过的梦 2024-10-04 22:58:04

拥有这两个独立通信通道的原因是我想让客户端成为多线程(一个线程扫描目录树,而另一个线程将更改的文件传输到服务器)。

这个推理不成立。即使您有一个线程在文件系统中徘徊寻找工作,您也可以使用在单个套接字上运行的单个协议。

不过,可能有其他原因导致您希望以不同于在客户端和服务器之间发送元数据或其他结构化数据的方式发送文件数据。然而,我想到的主要问题是您可能不希望强制命令等待文件完成,而 PB 的 FilePager 类可以缓解这个问题。

如果您要在使用 Twisted 的应用程序中拥有线程,需要记住的主要事情是,每当您想要从任何线程(“除了”运行反应器的线程之外)调用 Twisted API 时,您必须使用 reactor.callFromThread(或仅基于该方法构建的 API,例如 twisted.internet.threads.blockingCallFromThread)。

callFromThread 将一些工作(以要调用的对象的形式)发送到反应器线程,反应器将安排“很快”调用它。从错误线程调用的任何其他 Twisted API 都将产生未定义的结果。

The reason for having these two separate communication channels is down to the fact that I would like to make the client multi-threaded (one thread scanning the directory tree, while another thread transfers the changed files to the server).

This reasoning doesn't follow. You can use a single protocol running over a single socket just fine, even if you have a thread wandering the filesystem looking for work to do.

There may be other reasons to want to send file data differently than you send metadata or other structured data between the client and server, though. However, the main one that comes to mind is that you might not want to force commands to wait for files to be completed, and this issue is relieved by PB's FilePager class.

The main thing to remember if you're going to have threads in a Twisted-using application is that whenever you want to invoke a Twisted API from any thread ''except'' the thread in which the reactor is running you must use reactor.callFromThread (or an API built solely on that method, such as twisted.internet.threads.blockingCallFromThread).

callFromThread sends some work (in the form of an object to call) to the reactor thread where the reactor will arrange to call it "soon". Any other Twisted API you invoke from the wrong thread will have undefined results.

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