7

I'm currently experimenting with moving more of my development workflow into org-mode so I can keep better track of what testing I've done. However some of the steps involve asynchronous calls to RPC services. The following snippet shows the problem:

#+name: simple-async-example
#+begin_src emacs-lisp 
  (async-start
     ;; What to do in the child process
     (lambda ()
       (message "This is a test")
       (sleep-for 3)
       222)

     ;; What to do when it finishes
     (lambda (result)
       (format "Async process done - result should be 222: %s" result)))
#+end_src

#+RESULTS: simple-async-example
: #<process emacs>

As you can see the result comes from the first task. What I need to do is append the actual result once the processing has been completed. Is there any way to achieve this?

6
  • 1
    Apart from the title, it isn't actually clear what your question is. Answering the title, I think this question comes up often on the Org email list, and the answer is "not currently supported".
    – mankoff
    Commented Oct 21, 2014 at 19:39
  • @mankoff: OK I've re-worded and simplified the question to try an make it easier to follow. Is that better?
    – stsquad
    Commented Oct 22, 2014 at 9:05
  • en.wikipedia.org/wiki/Minimal_Working_Example why not have a 3-line bash script that returns something asynch that anyone can run/test/understand?
    – mankoff
    Commented Oct 22, 2014 at 13:52
  • @mankoff: well bash is inherently synchronous isn't it? I did wrap all the build and upload stuff into one script to simplify it but the LAVA job submission is something that happens asynchronously within emacs.
    – stsquad
    Commented Oct 22, 2014 at 14:22
  • @mankoff: How's the new elisp only example?
    – stsquad
    Commented Oct 22, 2014 at 14:34

1 Answer 1

4

OK Solving for the (async) example I've come up with this hack. It would be nice if I could automatically get the name of the source block rather than having to repeat it as a var.

** Post-modify the org-result
#+name: simple-async-with-post-modify
#+begin_src emacs-lisp :var this-buffer=(buffer-name (current-buffer)) this-name="simple-async-with-post-modify"
  (async-start
     ;; What to do in the child process
   `(lambda ()
     (message "This is a test")
     (sleep-for 3)
     ,(async-inject-variables "this-buffer")
     ,(async-inject-variables "this-name")
     (list
        (cons 'buffer this-buffer)
        (cons 'name this-name)
        (cons 'result 222)))

     ;; What to do when it finishes
   (lambda (result)
     (let ((buf (cdr (assoc 'buffer result)))
           (name (cdr (assoc 'name result)))
           (res (cdr (assoc 'result result))))
       (save-excursion
         (with-current-buffer buf
           (org-babel-goto-named-result name)
           (next-line)
           (goto-char (org-babel-result-end))
           (org-babel-insert-result (format "we got: %s" res)))))))
#+end_src

#+RESULTS: simple-async-with-post-modify
: #<process emacs>
: we got: 222
1
  • 1
    Hi. There is a post by John Kitchin discussing a prototype solution for python code blocks that might be interesting to you.
    – dfeich
    Commented Mar 1, 2016 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.