From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id KLoqG3SqUGHFwQAAgWs5BA (envelope-from ) for ; Sun, 26 Sep 2021 19:14:28 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id BVieFnSqUGGsbAAAbx9fmQ (envelope-from ) for ; Sun, 26 Sep 2021 17:14:28 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 4F45A2E3D1 for ; Sun, 26 Sep 2021 19:14:27 +0200 (CEST) Received: from localhost ([::1]:45260 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mUXjO-00017K-Gy for larch@yhetil.org; Sun, 26 Sep 2021 13:14:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:48116) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mUXiB-00016w-TL for emacs-orgmode@gnu.org; Sun, 26 Sep 2021 13:13:12 -0400 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]:39442) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mUXi9-0004as-K4 for emacs-orgmode@gnu.org; Sun, 26 Sep 2021 13:13:11 -0400 Received: by mail-wr1-x42d.google.com with SMTP id r23so19452002wra.6 for ; Sun, 26 Sep 2021 10:13:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:user-agent:mime-version; bh=686PiqqqA6mjFKndPRkfYXmq0ezT7aaupujF4IxNJdk=; b=GrdO2LljmkfrnE3UtjvHDKtsfRGeToIquTydQr5kgWR/+TbW3ZQr32i+SHQn2o5roc QXKLaQKFx+/iO/asla1JJArkuqhyj8eAM6hm1KNQpxRIlsNGdGi8pxZ4e8YQEYujLboK jrAx3H5naF2nUUuB3j2nZhquapJHy+hjynhWPzV3rvVoZJQiHFNduz4O0k2fxGovcAKs hqP+C0NpcevoAUofFfXKKC41b2QOl2sEOBMsMdPGZ40wMGtNEwO3psa8LqdaXLAQPu5v Uo1X69rTolstLTXW5OraDJXHdSWhV1yoq2nr68ZMQ2267w/E3gm2YNBJAubGBZFwMino cLIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:user-agent :mime-version; bh=686PiqqqA6mjFKndPRkfYXmq0ezT7aaupujF4IxNJdk=; b=z6FOMWWGYVSVf1t6svVEc7YZ0i7/QCTnlRPvEGNlpi7JmrTRppclNFz58i4nukazJa WyXHkh7stVAMEA5XAJ1UI+8vvXXOYvFhi45TLEnVInv0CWrq4Hkp7/E+MJmZgAQN6VwX NecxP0TJpmqpyf6PQnu+gMsgh4GgoaVr42SWLrnpXJRsEIH5ncb6rr/P/MyrX9xzLaTX QHOTzjfGY3kl7lYcXGwV2o0WVP9OGNfC6XUsga9Lkv/tSX1HIxeLH6fM/9OjyWKyumho oz2eWpX++Wg6jsckKH/tMzi7r1VSMz8JO5uXwkTUI1jsCkDQhzT33QPzW8LDfO0boqAB l0tg== X-Gm-Message-State: AOAM530pPh3UfGNpj8bsCJHvrpJ8lJrS8HqnLWuBGzfAMUm4MUnrrRDj T0L/tLt6CYHO6nIUAJRoNM2z3GbhoEQ937mm X-Google-Smtp-Source: ABdhPJzGQkoS4Qva7jPQSWGdN1cPe66qBCyOo+H94A2aVSmYTC0AXOcs6aOvIHCW1so2z+5hg01m9A== X-Received: by 2002:a1c:234a:: with SMTP id j71mr12220487wmj.11.1632676387319; Sun, 26 Sep 2021 10:13:07 -0700 (PDT) Received: from localhost (ip-95-223-72-103.hsi16.unitymediagroup.de. [95.223.72.103]) by smtp.gmail.com with ESMTPSA id l16sm5275272wmj.33.2021.09.26.10.13.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Sep 2021 10:13:06 -0700 (PDT) From: Jeremie Juste To: Org Mode Subject: [PATCH] async process in R Date: Sun, 26 Sep 2021 19:13:02 +0200 Message-ID: <87zgrzclb5.fsf@debian-BULLSEYE-live-builder-AMD64> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2a00:1450:4864:20::42d; envelope-from=jeremiejuste@gmail.com; helo=mail-wr1-x42d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jack Kamm , "Berry, Charles" Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1632676467; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=686PiqqqA6mjFKndPRkfYXmq0ezT7aaupujF4IxNJdk=; b=JRhHfvkHCYEaUk5tsDY03HUkqxA0a2VpnckKOJtgyTxPMYqs1+O96carRB5VzTlcgFeSzd abusMDgSsMcDsngOhXkN08+v0Mqv83MQteKMPZAvTohZOqtKUjAwXZMVmNgvfdKnBDpZeg tpE4PTmaPICNgH6F6VrH72JzDWnZy0IztqsybTiYFOeGhmDS17B9G6dqwwsKSTe572xMw4 zgd/Ipk2mg4YmWzMe2RPFFnmjq9kWk1KRqOE1xvXD6CznREc43bRIwKRn9y3KkStJfw+h2 gENMtRBW3D0rJ932fdYwvFBNav/SPMfPB0pJ3foAktBZuGGGiyy/Th4CR4qhyg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1632676467; a=rsa-sha256; cv=none; b=nGtBuDJWSueA08WZXbuTYGL3Po+VJQ/UMSlx77s42K7E7L/2ijzMFxWHrRsvlQqIL9iOW2 vcrv0Sff0gyYe0Myr5Rfz1gX5UdbnGUi3OA4KMLgmlEBP4lrcbYmXKvHg5AGL8s0RoGyHt CZpJM+ujkTj5rDVVn73py5pDy9ChprSg/QqxiVzk7N5PVeh5NKsy41FH8iCLAp9Tq+J/qT zB2aCqQtXYdvs1tJdlU+K0WzsJNLchToXYykWGhcaoXJwmXyz35j1Ag/Xt9qpUPnvlYZKN THnJYsYtiTb5z8LXsGk6rw471KaIt26SXQblYEuzoxb9+tuPTD8C1Jj5GQAhVQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=GrdO2Llj; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Spam-Score: -2.59 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=GrdO2Llj; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Queue-Id: 4F45A2E3D1 X-Spam-Score: -2.59 X-Migadu-Scanner: scn0.migadu.com X-TUID: W5rXWcVbI/Ub --=-=-= Content-Type: text/plain Hello, I have integrated some of the ob-session-async package in ob-R.el (finally). Most of the heavy lifting has been made by Jack. So with the patch async evaluation is expected to work out of the box for :result value #+begin_src R :session *R* :results value :async :colnames yes Sys.sleep(10) as.list(1:5) #+end_src #+RESULTS: | x | |---| | 1 | | 2 | | 3 | | 4 | | 5 | But for the time being result output produces the following output. #+begin_src R :session *R* :results output :async Sys.sleep(1) print(1:5) #+end_src #+RESULTS: : > Sys.sleep(1) : > print(1:5) : [1] 1 2 3 4 5 : > 'ob_comint_async_R_end_53c0a42fed17cf78f5508e5293029430' >From my understanding the async command of python does not suffer from this issue. I'm not sure if the issue needs to be solve at the ob-R.el or at the ob-core.el. I welcome any comments on this patch or any idea to move it forward. Best regards, Jeremie --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-lisp-ob-R-Async-evaluation-in-R.patch Content-Description: async in ob-R >From 51e1efb75e15fab348fd5a2c8b5fb5c1dbbf4d1c Mon Sep 17 00:00:00 2001 From: Jeremie Juste Date: Sun, 26 Sep 2021 18:25:23 +0200 Subject: [PATCH] lisp/ob-R: Async evaluation in R * lisp/ob-R.el `ob-session-async-R-indicator': Add constant representing a prefix R to identity session. (ob-session-async-org-babel-R-evaluate-session):New function to evaluate R src block asynchrously. (ob-session-async-R-value-callback): New function that callback the result of the asynchronous evaluation. (org-babel-R-evaluate): Add `async' parameter and call ob-session-async-org-babel-R-evaluate-session if `async' parameter is present. (org-babel-execute:R): Call (org-babel-comint-use-async) to check if async is among `params' and add async parameter to (org-babel-R-evaluate). * testing/lisp/test-ob-R.el: Add 7 more tests for async evaluations, also taken from ob-session-async package. This is almost a carbon copy of the package of ob-session-async of Jack Kamm. The original source code can be found https://github.com/jackkamm/ob-session-async Please refer to the following thread to trace back the discussion on async evaluation in R. https://list.orgmode.org/87eed9g9p6.fsf@gmail.com/ --- lisp/ob-R.el | 90 +++++++++++++++++++++++++++++++++++-- testing/lisp/test-ob-R.el | 94 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 181 insertions(+), 3 deletions(-) diff --git a/lisp/ob-R.el b/lisp/ob-R.el index 2d9073cee..299ccdf1d 100644 --- a/lisp/ob-R.el +++ b/lisp/ob-R.el @@ -158,6 +158,7 @@ This function is called by `org-babel-execute-src-block'." (save-excursion (let* ((result-params (cdr (assq :result-params params))) (result-type (cdr (assq :result-type params))) + (async (org-babel-comint-use-async params)) (session (org-babel-R-initiate-session (cdr (assq :session params)) params)) (graphics-file (and (member "graphics" (assq :result-params params)) @@ -184,7 +185,8 @@ This function is called by `org-babel-execute-src-block'." (cdr (assq :colname-names params)) colnames-p)) (or (equal "yes" rownames-p) (org-babel-pick-name - (cdr (assq :rowname-names params)) rownames-p))))) + (cdr (assq :rowname-names params)) rownames-p)) + async))) (if graphics-file nil result)))) (defun org-babel-prep-session:R (session params) @@ -371,11 +373,14 @@ Has four %s escapes to be filled in: 4. The name of the file to write to") (defun org-babel-R-evaluate - (session body result-type result-params column-names-p row-names-p) + (session body result-type result-params column-names-p row-names-p async) "Evaluate R code in BODY." (if session + (if async + (ob-session-async-org-babel-R-evaluate-session + session body result-type result-params column-names-p row-names-p) (org-babel-R-evaluate-session - session body result-type result-params column-names-p row-names-p) + session body result-type result-params column-names-p row-names-p)) (org-babel-R-evaluate-external-process body result-type result-params column-names-p row-names-p))) @@ -468,6 +473,85 @@ Insert hline if column names in output have been requested." (error "Could not parse R result")) result)) + +;;; async evaluation + +(defconst ob-session-async-R-indicator "'ob_comint_async_R_%s_%s'") + +(defun ob-session-async-org-babel-R-evaluate-session + (session body result-type result-params column-names-p row-names-p) + "Asynchronously evaluate BODY in SESSION. +Returns a placeholder string for insertion, to later be replaced +by `org-babel-comint-async-filter'." + (org-babel-comint-async-register + session (current-buffer) + "^\\(?:[>.+] \\)*\\[1\\] \"ob_comint_async_R_\\(.+?\\)_\\(.+\\)\"$" + 'org-babel-chomp + 'ob-session-async-R-value-callback) + (cl-case result-type + (value + (let ((tmp-file (org-babel-temp-file "R-"))) + (with-temp-buffer + (insert + (org-babel-chomp body)) + (let ((ess-local-process-name + (process-name (get-buffer-process session)))) + (ess-eval-buffer nil))) + (with-temp-buffer + (insert + (mapconcat + 'org-babel-chomp + (list (format org-babel-R-write-object-command + (if row-names-p "TRUE" "FALSE") + (if column-names-p + (if row-names-p "NA" "TRUE") + "FALSE") + ".Last.value" + (org-babel-process-file-name tmp-file 'noquote)) + (format ob-session-async-R-indicator + "file" tmp-file)) + "\n")) + (let ((ess-local-process-name + (process-name (get-buffer-process session)))) + (ess-eval-buffer nil))) + tmp-file)) + (output + (let ((uuid (md5 (number-to-string (random 100000000)))) + (ess-local-process-name + (process-name (get-buffer-process session)))) + (with-temp-buffer + (insert (format ob-session-async-R-indicator + "start" uuid)) + (insert "\n") + (insert body) + (insert "\n") + (insert (format ob-session-async-R-indicator + "end" uuid)) + (ess-eval-buffer nil)) + uuid)))) + +(defun ob-session-async-R-value-callback (params tmp-file) + "Callback for async value results. +Assigned locally to `ob-session-async-file-callback' in R +comint buffers used for asynchronous Babel evaluation." + (let* ((graphics-file (and (member "graphics" (assq :result-params params)) + (org-babel-graphical-output-file params))) + (colnames-p (unless graphics-file (cdr (assq :colnames params))))) + (org-babel-R-process-value-result + (org-babel-result-cond (assq :result-params params) + (with-temp-buffer + (insert-file-contents tmp-file) + (org-babel-chomp (buffer-string) "\n")) + (org-babel-import-elisp-from-file tmp-file '(16))) + (or (equal "yes" colnames-p) + (org-babel-pick-name + (cdr (assq :colname-names params)) colnames-p))))) + + + +;;; ob-session-async-R.el ends here + + (provide 'ob-R) ;;; ob-R.el ends here diff --git a/testing/lisp/test-ob-R.el b/testing/lisp/test-ob-R.el index c36bac9e6..1e60ae903 100644 --- a/testing/lisp/test-ob-R.el +++ b/testing/lisp/test-ob-R.el @@ -117,6 +117,8 @@ x )))) + + ;; (ert-deftest test-ob-r/output-with-error () ;; "make sure angle brackets are well formatted" ;; (let (ess-ask-for-ess-directory ess-history-file) @@ -151,6 +153,98 @@ log10(10) #+end_src" (org-babel-execute-src-block)))))) + +(ert-deftest ob-session-async-R-simple-session-async-value () + (let (ess-ask-for-ess-directory + ess-history-file + (org-babel-temporary-directory "/tmp") + (org-confirm-babel-evaluate nil)) + (org-test-with-temp-text + "#+begin_src R :session R :async yes\n Sys.sleep(.1)\n paste(\"Yep!\")\n#+end_src\n" + (should (let ((expected "Yep!")) + (and (not (string= expected (org-babel-execute-src-block))) + (string= expected + (progn + (sleep-for 0 200) + (goto-char (org-babel-where-is-src-block-result)) + (org-babel-read-result))))))))) + +(ert-deftest ob-session-async-R-simple-session-async-output () + (let (ess-ask-for-ess-directory + ess-history-file + (org-babel-temporary-directory "/tmp") + (org-confirm-babel-evaluate nil)) + (org-test-with-temp-text + "#+begin_src R :session R :results output :async yes\n Sys.sleep(.1)\n 1:5\n#+end_src\n" + (should (let ((expected "[1] 1 2 3 4 5")) + (and (not (string= expected (org-babel-execute-src-block))) + (string= expected + (progn + (sleep-for 0 200) + (goto-char (org-babel-where-is-src-block-result)) + (org-babel-read-result))))))))) + +(ert-deftest ob-session-async-R-named-output () + (let (ess-ask-for-ess-directory + ess-history-file + (org-babel-temporary-directory "/tmp") + org-confirm-babel-evaluate + (src-block "#+begin_src R :async :session R :results output\n 1:5\n#+end_src") + (results-before "\n\n#+NAME: foobar\n#+RESULTS:\n: [1] 1") + (results-after "\n\n#+NAME: foobar\n#+RESULTS:\n: [1] 1 2 3 4 5\n")) + (org-test-with-temp-text + (concat src-block results-before) + (should (progn (org-babel-execute-src-block) + (sleep-for 0 200) + (string= (concat src-block results-after) + (buffer-string))))))) + +(ert-deftest ob-session-async-R-named-value () + (let (ess-ask-for-ess-directory + ess-history-file + org-confirm-babel-evaluate + (org-babel-temporary-directory "/tmp") + (src-block "#+begin_src R :async :session R :results value\n paste(\"Yep!\")\n#+end_src") + (results-before "\n\n#+NAME: foobar\n#+RESULTS:\n: [1] 1") + (results-after "\n\n#+NAME: foobar\n#+RESULTS:\n: Yep!\n")) + (org-test-with-temp-text + (concat src-block results-before) + (should (progn (org-babel-execute-src-block) + (sleep-for 0 200) + (string= (concat src-block results-after) + (buffer-string))))))) + +(ert-deftest ob-session-async-R-output-drawer () + (let (ess-ask-for-ess-directory + ess-history-file + org-confirm-babel-evaluate + (org-babel-temporary-directory "/tmp") + (src-block "#+begin_src R :async :session R :results output drawer\n 1:5\n#+end_src") + (result "\n\n#+RESULTS:\n:results:\n[1] 1 2 3 4 5\n:end:\n")) + (org-test-with-temp-text + src-block + (should (progn (org-babel-execute-src-block) + (sleep-for 0 200) + (string= (concat src-block result) + (buffer-string))))))) + +(ert-deftest ob-session-async-R-value-drawer () + (let (ess-ask-for-ess-directory + ess-history-file + org-confirm-babel-evaluate + (org-babel-temporary-directory "/tmp") + (src-block "#+begin_src R :async :session R :results value drawer\n 1:3\n#+end_src") + (result "\n\n#+RESULTS:\n:results:\n1\n2\n3\n:end:\n")) + (org-test-with-temp-text + src-block + (should (progn (org-babel-execute-src-block) + (sleep-for 0 200) + (string= (concat src-block result) + (buffer-string))))))) + + + + (provide 'test-ob-R) ;;; test-ob-R.el ends here -- 2.30.2 --=-=-=--