import functools


def lock_threads(method):
    """Make an non thread safe method lock and unlock the thread on enter and
    exit respectively."""

    @functools.wraps(method)
    def wrapped(self, *args, **kwargs):
        with self._thread_lock:
            return method(self, *args, **kwargs)

    return wrapped