Gentoo Archives: gentoo-portage-dev

From: Alec Warner <antarus@g.o>
To: Zac Medico <zmedico@g.o>
Cc: gentoo-portage-dev@l.g.o
Subject: Re: [gentoo-portage-dev] [PATCH] bindbapi: add reentrant lock method (bug 685236)
Date: Wed, 08 May 2019 18:09:34
Message-Id: CAAr7Pr9=yfZD6OvsOjiOosm2k+STx=cLuBJW-0dMzS58i8VRWw@mail.gmail.com
In Reply to: Re: [gentoo-portage-dev] [PATCH] bindbapi: add reentrant lock method (bug 685236) by Zac Medico
1 On Wed, May 8, 2019 at 2:05 PM Zac Medico <zmedico@g.o> wrote:
2
3 > On 5/7/19 1:01 PM, Zac Medico wrote:
4 > > On 5/7/19 7:55 AM, Alec Warner wrote:
5 > >
6 > >> Also curious why we are not implementing enter and exit so we can avoid
7 > >> unbalanced pairs by using context managers.
8 > >>
9 > >> e.g. in match(), we could likely write:
10 > >>
11 > >> with self.dbapi.lock():
12 > >> # try to match
13 > >> update_pkgindex = self._populate_local()
14 > >> if update_pkgindex:
15 > >> self._pkgindex_write(update_pkgindex)
16 > >>
17 > >> If the block raises an exception, __exit__ is still called, so we can
18 > >> eliminate the finally: blocks and replace them with a careful __exit__
19 > >> implementation?
20 > >>
21 > >> -A
22 > >
23 > > Yeah, the reference counted lock class could have a method that returns
24 > > a context manager which increments/decrements the lock count. The
25 > > context manager can be implemented with the contextlib.contextmanager
26 > > decorator like this:
27 > >
28 > > @contextlib.contextmanager
29 > > def contextmanager(self):
30 > > self.lock()
31 > > try:
32 > > yield self
33 > > finally:
34 > > self.unlock()
35 > >
36 >
37 > Since we really don't want asynchronous code to block waiting for a
38 > lock, we really should use a (python3.5+ only) asynchronous context
39 > manager:
40 >
41 >
42 > https://www.python.org/dev/peps/pep-0492/#asynchronous-context-managers-and-async-with
43 >
44 > Given that python2.7 is scheduled for retirement in 2020
45 > (https://pythonclock.org/), maybe it's time to drop support for
46 > python3.4 and earlier.
47 >
48
49 I haven't used that stuff yet, so I will defer to you on its use; same with
50 python support.
51
52 -A
53
54
55 > --
56 > Thanks,
57 > Zac
58 >
59 >