From de2d2d928fce0d8652a90f29e7c95a232524fa88 Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Tue, 15 Nov 2022 11:39:04 +0800 Subject: [PATCH] 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 c6f93a609..1829ab149 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)))