def _handler(self):

        resp_timeout = self.resp_timeout

        threading.currentThread().setName(self.identifier + 'handler')

        time.sleep(0.1)   # Don't start handling before main thread ready

        if __debug__: self._log(1, 'starting')

        typ, val = self.abort, 'connection terminated'

        while not self.Terminate:

            self.idle_lock.acquire()
            if self.idle_timeout is not None:
                timeout = self.idle_timeout - time.time()
                if timeout <= 0:
                    timeout = 1
                if __debug__:
                    if self.idle_rqb is not None:
                        self._log(5, 'server IDLING, timeout=%.2f' % timeout)
            else:
                timeout = resp_timeout
            self.idle_lock.release()

            try:
                line = self.inq.get(True, timeout)
            except queue.Empty:
                if self.idle_rqb is None:
                    if resp_timeout is not None and self.tagged_commands:
                        if __debug__: self._log(1, 'response timeout')
                        typ, val = self.abort, 'no response after %s secs' % resp_timeout
                        break
                    continue
                if self.idle_timeout > time.time():
                    continue
                if __debug__: self._log(2, 'server IDLE timedout')
                line = IDLE_TIMEOUT_RESPONSE

            if line is None:
                if __debug__: self._log(1, 'inq None - terminating')
                break

            if not isinstance(line, bytes):
                typ, val = line
                break

            try:
                self._put_response(line)
            except:
                typ, val = self.error, 'program error: %s - %s' % sys.exc_info()[:2]
                break

        self.Terminate = True

        if __debug__: self._log(1, 'terminating: %s' % repr(val))

        while not self.ouq.empty():
            try:
                qel = self.ouq.get_nowait()
                if qel is not None:
                    qel.abort(typ, val)
            except queue.Empty:
                break
        self.ouq.put(None)

        self.commands_lock.acquire()
        for name in list(self.tagged_commands.keys()):
            rqb = self.tagged_commands.pop(name)
            rqb.abort(typ, val)
        self.state_change_free.set()
        self.commands_lock.release()
        if __debug__: self._log(3, 'state_change_free.set')

        if __debug__: self._log(1, 'finished')
dataset/ETHPy150Open OfflineIMAP/imapfw/imapfw/imap/imaplib3/imaplib2.py/IMAP4._handler/original