[英]While loop for 10 seconds with blocking
I have a loop that should run for 10 seconds only, but inside the loop I'm using a blocking function, so the loop doesn't break after 10 seconds, but only after blocking function.我有一个应该只运行 10 秒的循环,但在循环内我使用的是阻塞 function,所以循环在 10 秒后不会中断,但只有在阻塞 function 之后才会中断。
endtime = time.time() + 10
while time.time() < endtime:
clientsocket, address = tcpServer.accept() # blocking
print("got another client!")
name = clientsocket.recv(1024)
print("bye")
I want to leave the loop after 10 seconds, but if I got into it before 10 seconds passed and no one is trying to connect I'm not getting out.我想在 10 秒后离开循环,但如果我在 10 秒过去之前进入它并且没有人试图连接,我就不会离开。
How can I solve this?我该如何解决这个问题?
You should set timeout in tcp instance before while like below:您应该在 tcp 实例之前设置超时,如下所示:
tcpServer.settimeout(10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.