Gentoo Archives: gentoo-portage-dev

From: Zac Medico <zmedico@g.o>
To: gentoo-portage-dev@l.g.o
Cc: Zac Medico <zmedico@g.o>
Subject: [gentoo-portage-dev] [PATCH] portage.getpid: call os.getpid() lazily
Date: Sat, 30 Jan 2021 12:59:57
Message-Id: 20210130125932.369681-1-zmedico@gentoo.org
1 Call os.getpid() lazily, which eliminates getpid calls when possible
2 after os.fork() in the portage.process module.
3
4 Bug: https://bugs.gentoo.org/767913
5 Signed-off-by: Zac Medico <zmedico@g.o>
6 ---
7 lib/portage/__init__.py | 4 +++-
8 1 file changed, 3 insertions(+), 1 deletion(-)
9
10 diff --git a/lib/portage/__init__.py b/lib/portage/__init__.py
11 index 3c9f78497..24c9d8b89 100644
12 --- a/lib/portage/__init__.py
13 +++ b/lib/portage/__init__.py
14 @@ -375,7 +375,7 @@ _sync_mode = False
15 class _ForkWatcher:
16 @staticmethod
17 def hook(_ForkWatcher):
18 - _ForkWatcher.current_pid = _os.getpid()
19 + _ForkWatcher.current_pid = None
20 # Force instantiation of a new event loop policy as a workaround
21 # for https://bugs.python.org/issue22087.
22 asyncio.set_event_loop_policy(None)
23 @@ -388,6 +388,8 @@ def getpid():
24 """
25 Cached version of os.getpid(). ForkProcess updates the cache.
26 """
27 + if _ForkWatcher.current_pid is None:
28 + _ForkWatcher.current_pid = _os.getpid()
29 return _ForkWatcher.current_pid
30
31 def _get_stdin():
32 --
33 2.26.2

Replies