如何减少 Twisted 服务器的内存使用量?

发布于 2024-08-10 16:37:34 字数 377 浏览 2 评论 0原文

我用 Python/Twisted 编写了一个音频广播服务器。工作正常,但是内存使用量增长太快了!我认为这是因为某些用户的网络可能不够好,无法及时下载音频。

我的音频服务器将音频数据广播到不同听众的客户端,如果其中一些客户端无法及时下载音频,则意味着我的服务器会保留音频数据,直到听众收到为止。更重要的是,我的音频服务器是一个广播服务器,它接收音频数据,并将它们发送到不同的客户端,我虽然 Twisted 将这些数据复制到不同的缓冲区中,即使它们是相同的音频片段。

我想减少内存使用量,所以我需要知道客户端何时收到音频,以便我可以决定何时丢弃一些慢速客户端。但我不知道如何用 Twisted 来实现这一点。有人有想法吗?

我还能做些什么来减少内存使用量?

谢谢。 维克多·林.

I wrote an audio broadcasting server with Python/Twisted. It works fine, but the usage of memory grows too fast! I think that's because some user's network might not be good enough to download the audio in time.

My audio server broadcast audio data to different listener's client, if some of them can't download the audio in time, that means, my server keep the audio data until listeners received. And what's more, my audio server is a broadcasting server, it receive audio data, and send them to different clients, I though Twisted copy those data in different buffer, even they are same audio piece.

I want to reduce the usage of memory usage, so I need to know when is the audio received by the client, so that I can decide when to discard some slow clients. But I have no idea how to achieve that with Twisted. Do anyone have idea?

And what else can I do to reduce usage of memory usage?

Thanks.
Victor Lin.

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

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

发布评论

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

评论(2

迷荒 2024-08-17 16:37:34

你没有说,但我假设你使用的是 TCP。编写一个内存不断增加的基于 UDP 的系统会很困难,因为客户端接收数据的速度无法赶上发送数据的速度。

TCP 具有内置的流量控制功能。如果接收方无法像您希望发送的那样快地读取数据,则您将可以使用此信息,并且您可以放慢速度发送。 BSD 套接字 API 的工作方式是 send(2) 调用将阻塞或返回 0 以指示它无法向发送缓冲区添加任何字节。 Twisted 中的工作方式是通过一个称为“生产者和消费者”的系统。该系统的要点是向消费者注册生产者。生产者重复调用消费者的write。当消费者无法跟上时,它会对生产者调用 pauseProducing。当消费者再次准备好获取更多数据时,它会对生产者调用 resumeProducing

您可以在生产者/消费者指南,Twisted 文档的一部分。

You didn't say, but I'm going to assume that you're using TCP. It would be hard to write a UDP-based system which had ever increasing memory because of clients who can't receive data as fast as you're trying to send it.

TCP has built-in flow control capabilities. If a receiver cannot read data as fast as you'd like to send it, this information will be made available to you and you can send more slowly. The way this works with the BSD socket API is that a send(2) call will block or will return 0 to indicate it cannot add any bytes to the send buffer. The way it works in Twisted is by a system called "producers and consumers". The gist of this system is that you register a producer with a consumer. The producer calls write on the consumer repeatedly. When the consumer cannot keep up, it calls pauseProducing on the producer. When the consumer is again ready for more data, it calls resumeProducing on the producer.

You can read about this system in more detail in the producer/consumer howto, part of Twisted's documentation.

虐人心 2024-08-17 16:37:34

确保您使用的是 Python 的 垃圾收集器,然后检查并删除您所在的变量不使用。

Make sure you're using Python's garbage collector and then go through and delete variables you aren't using.

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