how to do non blocking accept() in 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()
精彩评论