Use with section for locking threadsafe region (instead of raw calls to acquire/release)
This commit is contained in:
parent
080ab9a626
commit
7970fca93a
2 changed files with 48 additions and 52 deletions
|
|
@ -145,52 +145,50 @@ 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:
|
||||||
logger.error("Something went wrong in select")
|
logger.error("Something went wrong in select")
|
||||||
fnd_smth = False
|
fnd_smth = False
|
||||||
# Looking for invalid server
|
# Looking for invalid server
|
||||||
for r in _rlist:
|
for r in _rlist:
|
||||||
if not hasattr(r, "fileno") or not isinstance(r.fileno(), int) or r.fileno() < 0:
|
if not hasattr(r, "fileno") or not isinstance(r.fileno(), int) or r.fileno() < 0:
|
||||||
_rlist.remove(r)
|
_rlist.remove(r)
|
||||||
logger.error("Found invalid object in _rlist: " + str(r))
|
logger.error("Found invalid object in _rlist: " + str(r))
|
||||||
fnd_smth = True
|
fnd_smth = True
|
||||||
for w in _wlist:
|
for w in _wlist:
|
||||||
if not hasattr(w, "fileno") or not isinstance(w.fileno(), int) or w.fileno() < 0:
|
if not hasattr(w, "fileno") or not isinstance(w.fileno(), int) or w.fileno() < 0:
|
||||||
_wlist.remove(w)
|
_wlist.remove(w)
|
||||||
logger.error("Found invalid object in _wlist: " + str(w))
|
logger.error("Found invalid object in _wlist: " + str(w))
|
||||||
fnd_smth = True
|
fnd_smth = True
|
||||||
for x in _xlist:
|
for x in _xlist:
|
||||||
if not hasattr(x, "fileno") or not isinstance(x.fileno(), int) or x.fileno() < 0:
|
if not hasattr(x, "fileno") or not isinstance(x.fileno(), int) or x.fileno() < 0:
|
||||||
_xlist.remove(x)
|
_xlist.remove(x)
|
||||||
logger.error("Found invalid object in _xlist: " + str(x))
|
logger.error("Found invalid object in _xlist: " + str(x))
|
||||||
fnd_smth = True
|
fnd_smth = True
|
||||||
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:
|
||||||
try:
|
|
||||||
x.exception()
|
|
||||||
except:
|
|
||||||
logger.exception("Uncatched exception on server exception")
|
|
||||||
for w in wl:
|
|
||||||
try:
|
|
||||||
w.write_select()
|
|
||||||
except:
|
|
||||||
logger.exception("Uncatched exception on server write")
|
|
||||||
for r in rl:
|
|
||||||
for i in r.read():
|
|
||||||
try:
|
try:
|
||||||
self.receive_message(r, i)
|
x.exception()
|
||||||
except:
|
except:
|
||||||
logger.exception("Uncatched exception on server read")
|
logger.exception("Uncatched exception on server exception")
|
||||||
|
for w in wl:
|
||||||
|
try:
|
||||||
|
w.write_select()
|
||||||
|
except:
|
||||||
|
logger.exception("Uncatched exception on server write")
|
||||||
|
for r in rl:
|
||||||
|
for i in r.read():
|
||||||
|
try:
|
||||||
|
self.receive_message(r, i)
|
||||||
|
except:
|
||||||
|
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:
|
||||||
|
|
|
||||||
|
|
@ -71,17 +71,15 @@ 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)
|
||||||
if self in _wlist:
|
if self in _wlist:
|
||||||
_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
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue