开发者

how to do non blocking accept() in Python?

开发者_如何学Python

I cannot use threads thus I want to write a server program that can be interrupted after a while:

d = show_non_modal_dialog("serving clients")
s = socket(...)
s.bind(...)
s.listen()
while (!user_pressed_cancel())
{
  s.accept() # timed accept for like 1 second
  if timed_out:
    continue
  serve_client
  close_client_sock
}
hide_non_modal_dialog(d)


Use a non-blocking socket and call accept on that.

s.setblocking(0)

You could also set a timeout for blocking socket operations

socket.settimeout(value)

There also seems to be an issue in your code

accept() returns a (conn, address) pair value. so your code should have been

conn, address = s.accept()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜