Skip to content

Commit

Permalink
web: fix 100% CPU
Browse files Browse the repository at this point in the history
  • Loading branch information
borisfaure committed Jun 27, 2010
1 parent c2687b1 commit 628e0e3
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions amsn2/ui/front_ends/web/bend.py
Expand Up @@ -43,18 +43,33 @@ def __init__(self, backend, socket, peer, rules):

self._rules = rules

gobject.io_add_watch(socket, gobject.IO_IN, self.on_read)
self._in_id = gobject.io_add_watch(socket.fileno(),
gobject.IO_IN | gobject.IO_PRI,
self.on_read)
self._out_id = gobject.io_add_watch(self._socket.fileno(),
gobject.IO_OUT,
self.on_write)
self._err_id = gobject.io_add_watch(self._socket.fileno(),
gobject.IO_ERR,
self.on_error)
self._hup_id = gobject.io_add_watch(self._socket.fileno(),
gobject.IO_HUP,
self.on_hang_up)


def close(self):
if self._is_alive:
self._is_alive = False
gobject.source_remove(self._in_id)
gobject.source_remove(self._out_id)
gobject.source_remove(self._err_id)
gobject.source_remove(self._hup_id)
self._socket.close()
self._socket = None

def write(self, data):
if self._is_alive:
self._wbuf += data
gobject.io_add_watch(self._socket, gobject.IO_OUT, self.on_write)
self.on_write(self._socket, gobject.IO_OUT)

def on_headers(self, headers):
Expand Down Expand Up @@ -96,6 +111,7 @@ def on_body(self, body):
pass

def on_read(self, s, c):
print "on read"
try:
chunk = self._socket.recv(READ_CHUNK_SIZE)
except socket.error, e:
Expand Down Expand Up @@ -126,6 +142,7 @@ def on_read(self, s, c):
return self._is_alive

def on_write(self, s, c):
print "on write"
while self._wbuf:
try:
b = self._socket.send(self._wbuf)
Expand All @@ -140,6 +157,16 @@ def on_write(self, s, c):
return self._is_alive
return self._is_alive

def on_error(self, s, c):
print "error"
self.close()
return self._is_alive

def on_hang_up(self, s, c):
print "hang up"
self.close()
return self._is_alive

def send_file(self, path):
f = open(BASEPATH + path, "r")
r = f.read()
Expand Down

0 comments on commit 628e0e3

Please sign in to comment.