Python/Twisted 多用户服务器 - 哪个更高效?

发布于 2024-07-13 04:21:53 字数 314 浏览 14 评论 0原文

在 Python 中,如果我希望我的服务器在 CPU 方面能够很好地扩展,我显然需要生成多个进程。 我想知道哪个更好(使用 Twisted):

A)管理器进程(持有实际套接字连接的进程)将接收到的数据包放入共享队列(来自多处理模块的队列),工作进程将数据包从队列,处理它们并将结果发送回客户端。

B) 管理进程(持有实际套接字连接的进程)启动一个延迟线程,然后调用进程池上的 apply() 函数。 一旦结果从工作进程返回,管理器将结果发送回客户端。

在这两种实现中,工作进程都使用线程池,因此它们可以同时处理多个数据包(因为会有大量数据库查询)。

In Python, if I want my server to scale well CPU-wise, I obviously need to spawn multiple processes. I was wondering which is better (using Twisted):

A) The manager process (the one who holds the actual socket connections) puts received packets into a shared queue (the one from the multiprocessing module), and worker processes pull the packets out of the queue, process them and send the results back to the client.

B) The manager process (the one who holds the actual socket connections) launches a deferred thread and then calls the apply() function on the process pool. Once the result returns from the worker process, the manager sends the result back to the client.

In both implementations, the worker processes use thread pools so they can work on more than one packet at once (since there will be a lot of database querying).

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

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

发布评论

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

评论(2

飘过的浮云 2024-07-20 04:21:53

我认为B有问题。 线程只会运行在一个CPU上,即使运行了一个进程,线程仍然在运行。 A可能会更好。

最好尝试从时间角度进行衡量,看看哪一个更快,哪一个扩展性更好。 然而,我要重申,我非常怀疑 B 能否很好地扩展。

I think that B is problematic. The thread would only run on one CPU, and even if it runs a process, the thread is still running. A may be better.

It is best to try and measure both in terms of time and see which one is faster and which one scales well. However, I'll reiterate that I highly doubt that B will scale well.

往事随风而去 2024-07-20 04:21:53

我认为“A”就是你想要的答案,但你不必自己做。

您考虑过安瓿吗?

I think that "A" is the answer you want, but you don't have to do it yourself.

Have you considered ampoule?

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