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.
This commit is contained in:
Ihor Radchenko 2022-11-15 11:39:04 +08:00
parent 4f88116e52
commit de2d2d928f
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
1 changed files with 18 additions and 2 deletions

View File

@ -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)))