1 |
On 5/7/19 1:01 PM, Zac Medico wrote: |
2 |
> On 5/7/19 7:55 AM, Alec Warner wrote: |
3 |
> |
4 |
>> Also curious why we are not implementing enter and exit so we can avoid |
5 |
>> unbalanced pairs by using context managers. |
6 |
>> |
7 |
>> e.g. in match(), we could likely write: |
8 |
>> |
9 |
>> with self.dbapi.lock(): |
10 |
>> # try to match |
11 |
>> update_pkgindex = self._populate_local() |
12 |
>> if update_pkgindex: |
13 |
>> self._pkgindex_write(update_pkgindex) |
14 |
>> |
15 |
>> If the block raises an exception, __exit__ is still called, so we can |
16 |
>> eliminate the finally: blocks and replace them with a careful __exit__ |
17 |
>> implementation? |
18 |
>> |
19 |
>> -A |
20 |
> |
21 |
> Yeah, the reference counted lock class could have a method that returns |
22 |
> a context manager which increments/decrements the lock count. The |
23 |
> context manager can be implemented with the contextlib.contextmanager |
24 |
> decorator like this: |
25 |
> |
26 |
> @contextlib.contextmanager |
27 |
> def contextmanager(self): |
28 |
> self.lock() |
29 |
> try: |
30 |
> yield self |
31 |
> finally: |
32 |
> self.unlock() |
33 |
> |
34 |
|
35 |
Since we really don't want asynchronous code to block waiting for a |
36 |
lock, we really should use a (python3.5+ only) asynchronous context manager: |
37 |
|
38 |
https://www.python.org/dev/peps/pep-0492/#asynchronous-context-managers-and-async-with |
39 |
|
40 |
Given that python2.7 is scheduled for retirement in 2020 |
41 |
(https://pythonclock.org/), maybe it's time to drop support for |
42 |
python3.4 and earlier. |
43 |
-- |
44 |
Thanks, |
45 |
Zac |