非阻塞调用的睡眠

发布于 12-04 10:25 字数 316 浏览 5 评论 0原文

我正在寻找最佳的睡眠价值,以从非阻止插座接收数据。例如:

while True:
    data=s.recv(1024)
    if not data:
        time.sleep(10) #10ms
    else:
        pass #...

没有睡眠会导致100%的CPU使用情况,所以知道如何获得最佳的CPU使用和频段?睡眠需要多长时间,以便CPU可以进行线程开关?

顺便说一句,通过so_sndbuf/so_recvbuf设置插座的缓冲区并设置tcp_nodelay还是不应该合并它们是有意义的吗?

I am looking for an optimum sleep value to receive data from a non-blocking socket. E.g:

while True:
    data=s.recv(1024)
    if not data:
        time.sleep(10) #10ms
    else:
        pass #...

No sleep would lead into 100% CPU usage, so any idea how to get the best CPU Usage and bandwith? How long has a sleep to be so the CPU can do a thread switch?

Btw, does it make sense to set the buffer of the socket via SO_SNDBUF/SO_RECVBUF and set TCP_NODELAY or shouldn't they be combined?

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

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

发布评论

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

评论(3

梦毁影碎の2024-12-11 10:25:51

如果您打算使用 sleep(),为什么不直接使用阻塞套接字呢?

If you plan to use sleep(), why don't you just use blocking sockets?

三月梨花2024-12-11 10:25:51

你自己不应该这样做。如果您需要代码经常唤醒,请使用 select 调用,并设置超时即使没有收到数据。

顺便说一句,TCP_NODELAY 是发送方的,不会影响您的读取

You shouldn't be doing that yourself. Use the select call, with a timeout if you need your code to wake up every so often even if no data was received.

BTW, TCP_NODELAY is of the sending side, won't influence your reads.

撧情箌佬2024-12-11 10:25:51

使用选择。它基本上会暂停程序并在套接字上有可用数据时将其唤醒。

Use select. It will basically pause the program and wake it up when there is data available on the socket.

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