emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/org de2d2d928f: ob-python: Wait for session initializat


From: ELPA Syncer
Subject: [elpa] externals/org de2d2d928f: ob-python: Wait for session initialization on slow machines
Date: Mon, 14 Nov 2022 22:57:54 -0500 (EST)

branch: externals/org
commit de2d2d928fce0d8652a90f29e7c95a232524fa88
Author: Ihor Radchenko <yantar92@posteo.net>
Commit: Ihor Radchenko <yantar92@posteo.net>

    ob-python: Wait for session initialization on slow machines
    
    * lisp/ob-python.el (org-babel-python--initialized): New internal flag
    used to indicate that python session has been initialized in buffer.
    (org-babel-python-initiate-session-by-key): Set
    `org-babel-python--initialized' in `python-shell-first-prompt-hook'
    when using built-in python.el.  Wait until the hook is fired before we
    finish initiating the session.
    
    This patch intends to fix CI test failures where the CPU allocation is
    limited and python loading is extremely slow.
---
 lisp/ob-python.el | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index c6f93a6095..1829ab149a 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -184,6 +184,8 @@ Emacs-lisp table, otherwise return the results as a string."
 
 (defvar py-which-bufname)
 (defvar python-shell-buffer-name)
+(defvar-local org-babel-python--initialized nil
+  "Flag used to mark that python session has been initialized.")
 (defun org-babel-python-initiate-session-by-key (&optional session)
   "Initiate a python session.
 If there is not a current inferior-process-buffer in SESSION
@@ -200,7 +202,14 @@ then create.  Return the initialized session."
          (setq py-buffer (org-babel-python-with-earmuffs session)))
        (let ((python-shell-buffer-name
               (org-babel-python-without-earmuffs py-buffer)))
-         (run-python cmd)))
+         (run-python cmd)
+          (with-current-buffer py-buffer
+            (add-hook
+             'python-shell-first-prompt-hook
+             (lambda ()
+               (setq-local org-babel-python--initialized t)
+               (message "I am running!!!"))
+             nil 'local))))
        ((and (eq 'python-mode org-babel-python-mode)
             (fboundp 'py-shell)) ; python-mode.el
        (require 'python-mode)
@@ -220,7 +229,14 @@ then create.  Return the initialized session."
        (t
        (error "No function available for running an inferior Python")))
       ;; Wait until Python initializes.
-      (org-babel-comint-wait-for-output py-buffer)
+      (if (eq 'python org-babel-python-mode) ; python.el
+          ;; This is more reliable compared to
+          ;; `org-babel-comint-wait-for-output' as python may emit
+          ;; multiple prompts during initialization.
+          (with-current-buffer py-buffer
+            (while (not org-babel-python--initialized)
+              (org-babel-comint-wait-for-output py-buffer)))
+        (org-babel-comint-wait-for-output py-buffer))
       (setq org-babel-python-buffers
            (cons (cons session py-buffer)
                  (assq-delete-all session org-babel-python-buffers)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]