Add sleep if no data is available to read, to prevent ridiculous CPU usage

develop
Sven Slootweg 13 years ago
parent 7cce3da297
commit 185af55f84

@ -23,21 +23,24 @@ client_map[client.ssl_sock.fileno()] = client.client
while True: while True:
readable, writable, error = select.select(select_inputs, select_outputs, select_inputs) readable, writable, error = select.select(select_inputs, select_outputs, select_inputs)
for sock in readable: if len(readable) > 0:
try: for sock in readable:
data = sock.recv(1024) try:
data = sock.recv(1024)
if data:
cur_client = client_map[sock.fileno()] if data:
cur_client.process_data(data) cur_client = client_map[sock.fileno()]
else: cur_client.process_data(data)
select_inputs = remove_from_list(select_inputs, sock) else:
print "NOTICE: Client disconnected" select_inputs = remove_from_list(select_inputs, sock)
except ssl.SSLError, err: print "NOTICE: Client disconnected"
if err.args[0] == ssl.SSL_ERROR_WANT_READ: except ssl.SSLError, err:
select.select([sock], [], []) if err.args[0] == ssl.SSL_ERROR_WANT_READ:
elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE: select.select([sock], [], [])
select.select([], [sock], []) elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:
else: select.select([], [sock], [])
raise else:
raise
else:
time.sleep(0.010)

@ -177,35 +177,38 @@ class SSLDaemon:
while self.select_inputs: while self.select_inputs:
readable, writable, error = select.select(self.select_inputs, self.select_outputs, self.select_inputs) readable, writable, error = select.select(self.select_inputs, self.select_outputs, self.select_inputs)
for sock in readable: if len(readable) > 0:
try: for sock in readable:
if sock is bindsocket: try:
newsocket, fromaddr = bindsocket.accept() if sock is bindsocket:
connstream = ssl.wrap_socket(newsocket, newsocket, fromaddr = bindsocket.accept()
server_side=True, connstream = ssl.wrap_socket(newsocket,
certfile=cert_path, server_side=True,
keyfile=key_path, certfile=cert_path,
ssl_version=ssl.PROTOCOL_TLSv1) keyfile=key_path,
ssl_version=ssl.PROTOCOL_TLSv1)
new_client = Client(connstream)
new_client = Client(connstream)
self.select_inputs.append(connstream)
self.select_outputs.append(connstream) self.select_inputs.append(connstream)
self.client_map[connstream.fileno()] = new_client self.select_outputs.append(connstream)
self.client_list.append(new_client) self.client_map[connstream.fileno()] = new_client
else: self.client_list.append(new_client)
data = sock.recv(1024)
if data:
cur_client = self.client_map[sock.fileno()]
cur_client.process_data(data)
else: else:
self.select_inputs = remove_from_list(self.select_inputs, sock) data = sock.recv(1024)
print "NOTICE: Client disconnected"
except ssl.SSLError, err: if data:
if err.args[0] == ssl.SSL_ERROR_WANT_READ: cur_client = self.client_map[sock.fileno()]
select.select([sock], [], []) cur_client.process_data(data)
elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE: else:
select.select([], [sock], []) self.select_inputs = remove_from_list(self.select_inputs, sock)
else: print "NOTICE: Client disconnected"
raise except ssl.SSLError, err:
if err.args[0] == ssl.SSL_ERROR_WANT_READ:
select.select([sock], [], [])
elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:
select.select([], [sock], [])
else:
raise
else:
time.sleep(0.010)

Loading…
Cancel
Save