[PATCH] threadutil: use 'with' statements for lock

Łukasz Żarnowiecki dolohow at outlook.com
Tue Jul 5 22:13:58 BST 2016


On Wed, Jun 29, 2016 at 01:56:36AM +0200, Nicolas Sebrecht wrote:
> diff --git a/offlineimap/threadutil.py b/offlineimap/threadutil.py
> index d5d08f8..29272db 100644
> --- a/offlineimap/threadutil.py
> +++ b/offlineimap/threadutil.py
> @@ -50,33 +50,24 @@ class accountThreads(object):
>          self.list = []
>  
>      def add(self, thread):
> -        self.lock.acquire()
> -        try:
> +        with self.lock:
>              self.list.append(thread)
> -        finally:
> -            self.lock.release()
>  
>      def remove(self, thread):
> -        self.lock.acquire()
> -        try:
> +        with self.lock:
>              self.list.remove(thread)
> -        finally:
> -            self.lock.release()
>  
>      def pop(self):
> -        self.lock.acquire()
> -        try:
> -            if not len(self.list):
> +        with self.lock:
> +            if len(self.list) < 1:
>                  return None
>              return self.list.pop()
> -        finally:
> -            self.lock.release()
>  
>      def wait(self):
> -        while 1:
> +        while True:
>              thread = self.pop()
> -            if not thread:
> -                return
> +            if thread is None:
> +                break
>              thread.join()
>  
>  

Definitely better approach.  Ship it!



More information about the OfflineIMAP-project mailing list