Use with section for locking threadsafe region (instead of raw calls to acquire/release)

This commit is contained in:
nemunaire 2015-09-05 10:14:10 +02:00
commit 7970fca93a
2 changed files with 48 additions and 52 deletions

View file

@ -145,7 +145,7 @@ class Bot(threading.Thread):
self.stop = False self.stop = False
while not self.stop: while not self.stop:
_lock.acquire() with _lock:
try: try:
rl, wl, xl = select(_rlist, _wlist, _xlist, 0.1) rl, wl, xl = select(_rlist, _wlist, _xlist, 0.1)
except: except:
@ -170,7 +170,6 @@ class Bot(threading.Thread):
if not fnd_smth: if not fnd_smth:
logger.exception("Can't continue, sorry") logger.exception("Can't continue, sorry")
self.quit() self.quit()
_lock.release()
continue continue
for x in xl: for x in xl:
@ -190,7 +189,6 @@ class Bot(threading.Thread):
except: except:
logger.exception("Uncatched exception on server read") logger.exception("Uncatched exception on server read")
_lock.release()
# Launch new consumer threads if necessary # Launch new consumer threads if necessary
while self.cnsr_queue.qsize() > self.cnsr_thrd_size: while self.cnsr_queue.qsize() > self.cnsr_thrd_size:

View file

@ -71,7 +71,7 @@ class AbstractServer(io.IOBase):
"""Generic close function that register the server un _{r,w,x}list in """Generic close function that register the server un _{r,w,x}list in
case of successful _close""" case of successful _close"""
self.logger.info("Closing connection to %s", self.id) self.logger.info("Closing connection to %s", self.id)
_lock.acquire() with _lock:
if not hasattr(self, "_close") or self._close(): if not hasattr(self, "_close") or self._close():
if self in _rlist: if self in _rlist:
_rlist.remove(self) _rlist.remove(self)
@ -79,9 +79,7 @@ class AbstractServer(io.IOBase):
_wlist.remove(self) _wlist.remove(self)
if self in _xlist: if self in _xlist:
_xlist.remove(self) _xlist.remove(self)
_lock.release()
return True return True
_lock.release()
return False return False