gpt4 book ai didi

python - (有)python socketserver 相对于常规套接字对象的性能优势吗?

转载 作者:太空狗 更新时间:2023-10-29 18:30:56 24 4
gpt4 key购买 nike

感谢迄今为止有趣的回复。鉴于上述答复,我稍微改变了我的问题。

猜猜我真正需要知道的是,socketserver 与旨在处理延迟和压力的直接套接字库相对,即它是否具有其他机制或功能来证明其隐式广告状态作为“服务器”,还是只是稍微更容易使用?

似乎每个人都在推荐 socketserver,但我仍然不完全清楚为什么,而不是 socket。

谢谢!!!

I've built some server programs in python based on the standard socket library http://docs.python.org/library/socket.html

I've noticed that they seem to work just fine except that without load they have a tendency to go to sleep after a while. I guess this may not be an issue in production (no doubt there will be plenty of other issues) but I would like to know if I am using the right code for the job here.

Looking around I saw that python also provides a socketserver library - http://docs.python.org/library/socketserver.html

The socket library provides the ability to listen for multiple connections, typically up to 5.

According to the socketserver page, its services are synchronous, i.e. blocking, but one may support asynchronous behavior via threading. I did notice it has the ability to maintain a request queue, with a default value of up to 5 requests...so maybe not much difference there.

I have also read that Twisted runs socketserver under the hood. Though I would rather not get into a beast the size of Twisted unless it's going to be worthwhile.

so my question is, is socketserver more robust than socket? If so, why?

(And how do you know?)

incidentally, is socketserver built on top of python's socket or is it entirely separate?

finally, as a bonus if anyone knows what one could do wrong such that standard sockets 'fall asleep' please feel free to chime in on that too.

Oh, and I'm talking python 2.x rather than 3.x here if that makes a difference.

thanks folks!

jsh


好吧,我没有技术上的答案,但我已经根据人们的建议实现了 SocketServer,它绝对更可靠。如果有人提出低级解释,请告诉我......谢谢!

最佳答案

套接字模块是一个非常低级的模块,用于发送和接收数据包。如文档中所述,它“提供对 BSD 套接字接口(interface)的访问”。

如果您想要更精细的东西,可以使用“socketserver”为您处理繁琐的细节,它的级别仍然相对较低。

最重要的是,您可以找到一个 HTTP 服务器,有或没有 CGI,一个 XML-RPC 服务器,等等。这些是框架,这通常意味着它们的代码会调用您的代码。它使事情变得更简单,因为你只需要填补一些“空白”就可以拥有一个完全工作的服务器,但这也意味着你对它所做的事情的控制有点少。

如果您只需要 socketserver 的功能,我可能会选择它,除非您出于某种原因想要重新发明轮子(并且设计新轮子总是有充分的理由,例如了解它的工作原理)。

关于python - (有)python socketserver 相对于常规套接字对象的性能优势吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6540284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com