ENH: removed double tab indentation, pass use_poll into loop, reorderd log msg to come after action to be factually correct

pull/1346/head
Yaroslav Halchenko 2016-03-09 22:17:14 -05:00
parent c84b6370c5
commit deca0b80ab
1 changed files with 26 additions and 22 deletions

View File

@ -92,26 +92,29 @@ class RequestHandler(asynchat.async_chat):
def loop(active, timeout=None, use_poll=False): def loop(active, timeout=None, use_poll=False):
# Use poll instead of loop, because of recognition of active flag, """Custom event loop implementation
# because of loop timeout mistake: different in poll and poll2 (sec vs ms),
# and to prevent sporadical errors like EBADF 'Bad file descriptor' etc. (see gh-161) Uses poll instead of loop to respect `active` flag,
if timeout is None: to avoid loop timeout mistake: different in poll and poll2 (sec vs ms),
timeout = Utils.DEFAULT_SLEEP_TIME and to prevent sporadic errors like EBADF 'Bad file descriptor' etc. (see gh-161)
poll = asyncore.poll """
if use_poll and asyncore.poll2 and hasattr(asyncore.select, 'poll'): # pragma: no cover if timeout is None:
logSys.debug('Server listener (select) uses poll') timeout = Utils.DEFAULT_SLEEP_TIME
# poll2 expected a timeout in milliseconds (but poll and loop in seconds): poll = asyncore.poll
timeout = float(timeout) / 1000 if use_poll and asyncore.poll2 and hasattr(asyncore.select, 'poll'): # pragma: no cover
poll = asyncore.poll2 logSys.debug('Server listener (select) uses poll')
# Poll as long as active: # poll2 expected a timeout in milliseconds (but poll and loop in seconds):
while active(): timeout = float(timeout) / 1000
try: poll = asyncore.poll2
poll(timeout) # Poll as long as active:
except Exception as e: # pragma: no cover while active():
if e.args[0] in (errno.ENOTCONN, errno.EBADF): # (errno.EBADF, 'Bad file descriptor') try:
logSys.info('Server connection was closed: %s', str(e)) poll(timeout)
else: except Exception as e: # pragma: no cover
logSys.error('Server connection was closed: %s', str(e)) if e.args[0] in (errno.ENOTCONN, errno.EBADF): # (errno.EBADF, 'Bad file descriptor')
logSys.info('Server connection was closed: %s', str(e))
else:
logSys.error('Server connection was closed: %s', str(e))
## ##
@ -177,7 +180,7 @@ class AsyncServer(asyncore.dispatcher):
# Sets the init flag. # Sets the init flag.
self.__init = self.__active = True self.__init = self.__active = True
# Event loop as long as active: # Event loop as long as active:
loop(lambda: self.__active) loop(lambda: self.__active, use_poll=use_poll)
# Cleanup all # Cleanup all
self.stop() self.stop()
@ -187,8 +190,8 @@ class AsyncServer(asyncore.dispatcher):
asyncore.dispatcher.close(self) asyncore.dispatcher.close(self)
# Remove socket (file) only if it was created: # Remove socket (file) only if it was created:
if self.__init and os.path.exists(self.__sock): if self.__init and os.path.exists(self.__sock):
logSys.debug("Removed socket file " + self.__sock)
os.remove(self.__sock) os.remove(self.__sock)
logSys.debug("Removed socket file " + self.__sock)
logSys.debug("Socket shutdown") logSys.debug("Socket shutdown")
self.__active = False self.__active = False
@ -198,6 +201,7 @@ class AsyncServer(asyncore.dispatcher):
def stop(self): def stop(self):
self.close() self.close()
# better remains a method (not a property) since used as a callable for wait_for
def isActive(self): def isActive(self):
return self.__active return self.__active