Gentoo Archives: gentoo-portage-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-portage-dev@l.g.o
Cc: Zac Medico <zmedico@g.o>
Subject: Re: [gentoo-portage-dev] [PATCH 1/3] Add cached portage.getpid() function
Date: Sat, 08 Aug 2020 05:51:23
Message-Id: 34629acf371e2bdcb02d3238de67251c347674a6.camel@gentoo.org
In Reply to: [gentoo-portage-dev] [PATCH 1/3] Add cached portage.getpid() function by Zac Medico
1 On Fri, 2020-08-07 at 21:08 -0700, Zac Medico wrote:
2 > Since getpid is a syscall, cache results, and update them
3 > via an after fork hook.
4 >
5 > Signed-off-by: Zac Medico <zmedico@g.o>
6 > ---
7 > lib/portage/__init__.py | 14 +++++++++++
8 > .../tests/process/test_AsyncFunction.py | 24 +++++++++++++++++++
9 > 2 files changed, 38 insertions(+)
10 >
11 > diff --git a/lib/portage/__init__.py b/lib/portage/__init__.py
12 > index 916c93510..52902ba7d 100644
13 > --- a/lib/portage/__init__.py
14 > +++ b/lib/portage/__init__.py
15 > @@ -14,6 +14,7 @@ try:
16 > if not hasattr(errno, 'ESTALE'):
17 > # ESTALE may not be defined on some systems, such as interix.
18 > errno.ESTALE = -1
19 > + import multiprocessing.util
20 > import re
21 > import types
22 > import platform
23 > @@ -368,6 +369,19 @@ _internal_caller = False
24 >
25 > _sync_mode = False
26 >
27 > +def _fork_watcher(_fork_watcher):
28 > + _fork_watcher.current_pid = _os.getpid()
29
30 I don't really like the idea of putting variables on functions. Would
31 there be any problem with using a proper class here?
32
33 > +
34 > +_fork_watcher(_fork_watcher)
35 > +
36 > +multiprocessing.util.register_after_fork(_fork_watcher, _fork_watcher)
37 > +
38 > +def getpid():
39 > + """
40 > + Cached version of os.getpid(). ForkProcess updates the cache.
41 > + """
42 > + return _fork_watcher.current_pid
43 > +
44 > def _get_stdin():
45 > """
46 > Buggy code in python's multiprocessing/process.py closes sys.stdin
47 > diff --git a/lib/portage/tests/process/test_AsyncFunction.py b/lib/portage/tests/process/test_AsyncFunction.py
48 > index 55857026d..3b360e02f 100644
49 > --- a/lib/portage/tests/process/test_AsyncFunction.py
50 > +++ b/lib/portage/tests/process/test_AsyncFunction.py
51 > @@ -3,6 +3,7 @@
52 >
53 > import sys
54 >
55 > +import portage
56 > from portage import os
57 > from portage.tests import TestCase
58 > from portage.util._async.AsyncFunction import AsyncFunction
59 > @@ -36,3 +37,26 @@ class AsyncFunctionTestCase(TestCase):
60 > def testAsyncFunctionStdin(self):
61 > loop = asyncio._wrap_loop()
62 > loop.run_until_complete(self._testAsyncFunctionStdin(loop))
63 > +
64 > + def _test_getpid_fork(self):
65 > + """
66 > + Verify that portage.getpid() cache is updated in a forked child process.
67 > + """
68 > + loop = asyncio._wrap_loop()
69 > + proc = AsyncFunction(scheduler=loop, target=portage.getpid)
70 > + proc.start()
71 > + proc.wait()
72 > + self.assertEqual(proc.pid, proc.result)
73 > +
74 > + def test_getpid_fork(self):
75 > + self._test_getpid_fork()
76 > +
77 > + def test_getpid_double_fork(self):
78 > + """
79 > + Verify that portage.getpid() cache is updated correctly after
80 > + two forks.
81 > + """
82 > + loop = asyncio._wrap_loop()
83 > + proc = AsyncFunction(scheduler=loop, target=self._test_getpid_fork)
84 > + proc.start()
85 > + self.assertEqual(proc.wait(), 0)
86
87 --
88 Best regards,
89 Michał Górny

Attachments

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