I am looking for an optimum sleep value to receive data from a non-blocking socket. Eg:
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?
如果您打算使用sleep()
,为什么不使用阻塞套接字呢?
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 read
s.
Use select
. It will basically pause the program and wake it up when there is data available on the socket.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.