Acquire a write lock for the current thread, waiting at most
        timeout seconds or doing a non-blocking check in case timeout
        is <= 0.

        In case timeout is None, the call to acquire_write blocks until
        the lock request can be serviced.

        If the lock has been successfully acquired, this function
        returns True. On a timeout it returns None. In case a trivial
        deadlock condition is detected (the current thread already hold
        a reader lock) it returns False.

def acquire_write(self, timeout=None):
        """
        Acquire a write lock for the current thread, waiting at most
        timeout seconds or doing a non-blocking check in case timeout
        is <= 0.

        In case timeout is None, the call to acquire_write blocks until
        the lock request can be serviced.

        If the lock has been successfully acquired, this function
        returns True. On a timeout it returns None. In case a trivial
        deadlock condition is detected (the current thread already hold
        a reader lock) it returns False.
        """
        if timeout is not None:
            endtime = time.time() + timeout
        me = threading.currentThread()
        self.__condition.acquire()
        try:
            if self.__writer is me:
                self.__writer_lock_count += 1
                return True
            if me in self.__readers:
                # trivial deadlock detected (we do not handle promotion)
                return False
            self.__pending_writers.append(me)
            while True:
                if not self.__readers and self.__writer is None and self.__pending_writers[0] is me:
                    self.__writer = me
                    self.__writer_lock_count = 1
                    self.__pending_writers = self.__pending_writers[1:]
                    return True
                if timeout is not None:
                    remaining = endtime - time.time()
                    if remaining <= 0:
                        self.__pending_writers.remove(me)
                        return None
                    self.__condition.wait(remaining)
                else:
                    self.__condition.wait()
        finally:
            self.__condition.release()