commit:     ef43dd8ef64ded5f0627b7b8e84ef21262012902
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Sat Jan 30 12:44:19 2021 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Mon Feb  8 04:47:20 2021 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=ef43dd8e

portage.getpid: call os.getpid() lazily

Call os.getpid() lazily, which eliminates getpid calls when possible
after os.fork() in the portage.process module.

Bug: https://bugs.gentoo.org/767913
Reviewed-by: Brian Dolbec <dolsen <AT> gentoo.org>
Signed-off-by: Zac Medico <zmedico <AT> gentoo.org>

 lib/portage/__init__.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/portage/__init__.py b/lib/portage/__init__.py
index 3c9f78497..24c9d8b89 100644
--- a/lib/portage/__init__.py
+++ b/lib/portage/__init__.py
@@ -375,7 +375,7 @@ _sync_mode = False
 class _ForkWatcher:
        @staticmethod
        def hook(_ForkWatcher):
-               _ForkWatcher.current_pid = _os.getpid()
+               _ForkWatcher.current_pid = None
                # Force instantiation of a new event loop policy as a workaround
                # for https://bugs.python.org/issue22087.
                asyncio.set_event_loop_policy(None)
@@ -388,6 +388,8 @@ def getpid():
        """
        Cached version of os.getpid(). ForkProcess updates the cache.
        """
+       if _ForkWatcher.current_pid is None:
+               _ForkWatcher.current_pid = _os.getpid()
        return _ForkWatcher.current_pid
 
 def _get_stdin():

Reply via email to