Gentoo Archives: gentoo-portage-dev

From: Zac Medico <zmedico@g.o>
To: gentoo-portage-dev@l.g.o, Alec Warner <antarus@g.o>
Cc: Zac Medico <zmedico@g.o>
Subject: Re: [gentoo-portage-dev] [PATCH] bindbapi: add reentrant lock method (bug 685236)
Date: Tue, 07 May 2019 20:01:24
Message-Id: 826ab0a5-ca7f-6af2-4474-749cfe580895@gentoo.org
In Reply to: Re: [gentoo-portage-dev] [PATCH] bindbapi: add reentrant lock method (bug 685236) by Alec Warner
1 On 5/7/19 7:55 AM, Alec Warner wrote:
2 >
3 >
4 > On Mon, May 6, 2019 at 8:15 PM Zac Medico <zmedico@g.o
5 > <mailto:zmedico@g.o>> wrote:
6 >
7 > Add a reentrant lock/unlock method which is useful for things like
8 > emaint binhost and eclean-pkg. The vardbapi class already provides
9 > lock/unlock methods that behave the same way.
10 >
11 >
12 > Curious why we are not doing more encapsulation here.
13 >
14 > I'd suggest a new class type in locks.py:
15 >
16 > class RefCountedLock(object): ...
17 > # This implements the Lock() / Unlock() interface, as well as perhaps
18 > __enter__ and __exit__.
19 > # It uses a lockfile at path and a reference count.
20 >
21 > Then in bintree's init, set self.lock to
22 > locks.refCountedLock(self.bintree._pkgindex_file)
23 >
24 > def lock(self): # on bintree
25 >   self._lock.Lock()
26 >
27 > def unlock(self):
28 >   self._lock.Unlock()
29
30 Yes, splitting out a reference counted lock class like this is a good
31 idea. I'll do that.
32
33 > Also curious why we are not implementing enter and exit so we can avoid
34 > unbalanced pairs by using context managers.
35 >
36 > e.g. in match(), we could likely write:
37 >
38 > with self.dbapi.lock():
39 >   # try to match
40 >   update_pkgindex = self._populate_local()
41 >   if update_pkgindex:
42 >     self._pkgindex_write(update_pkgindex)
43 >
44 > If the block raises an exception, __exit__ is still called, so we can
45 > eliminate the finally: blocks and replace them with a careful __exit__
46 > implementation?
47 >
48 > -A
49
50 Yeah, the reference counted lock class could have a method that returns
51 a context manager which increments/decrements the lock count. The
52 context manager can be implemented with the contextlib.contextmanager
53 decorator like this:
54
55 @contextlib.contextmanager
56 def contextmanager(self):
57 self.lock()
58 try:
59 yield self
60 finally:
61 self.unlock()
62
63 --
64 Thanks,
65 Zac

Attachments

File name MIME type
signature.asc application/pgp-signature

Replies