0

I am creating several org-mode documents to track code example and would like to be able to view the results of tangling or executing these code blocks in a browser. In the example below, I've created a source code block that starts a browsersync session; this which should reflect my changes in the web browser when I tangle a CSS or HTML file. How can I run this process in the background so it doesn't freeze Emacs? Currently, executing this code makes Emacs unusable in the same way that starting it in a shell would make the shell unusable.

#+BEGIN_SRC bash :tangle no :exports none
browser-sync start --server --browser "Google Chrome" --files "*.css, css3_by_example.html" &
#+END_SRC

3 Answers 3

3

You can check out ob-async. It worked well for me.

Someone said that you need to implement the feature for Emacs to do that. But ob-async does exactly what you want. It does run the process asynchronously so it doesn't freeze emacs. It's a package for babel and you don't need to write any LISP code. Just install ob-async using el-get or any other preferred method you're using. You can get it from melpa by typing the name ob-async with your fine keyboard. FYI, here's the melpa package link.

https://melpa.org/#/ob-async

How to use the package is already shown in the documentation of the package. My answer could be outdated in the future so please refer to the document provided by the package for your setting up.

At this time at early 2018, after you set up by (require 'ob-async) in your init script you can put :async in your code block. Then it will run asynchronously. Even if your process never ends, that should be okay.

I tried using other methods like using UNIX at command, but this way was the most cleanest way.

3
  • 1
    This is essentially a link-only answer (without even a link!), not a description of a solution. As such, it risks being deleted. Please consider elaborating.
    – Drew
    Mar 27, 2018 at 3:56
  • Thanks for the down voting. I think this is a really good answer and saves lots of time. I know link only answer like "I use this, it's cool" is bad especially when there are just many known choices. Regarding your concern I'm adding more boilerplate for you.
    – Jaehyun
    Mar 29, 2018 at 0:48
  • Thanks for elaborating - much more helpful now. (No one down-voted, BTW.)
    – Drew
    Mar 29, 2018 at 4:19
1

By default, Org src block evaluation is synchronous -- Emacs will wait for evaluation to finish, potentially forever. However, there are some very nice async functions built in to Emacs Lisp. You might consider creating a regular emacs lisp function built around start-process, which can manage your browser-sync session and let you preview your tangling. You could even use an non-tangling elisp src block to do the same thing, if you really want to keep it all in babel.

(There are also some approaches out there in Google Land for doing async block evaluation in babel, but I haven't evaluated any of it.)

0

I use the following to serve local web pages using python. I need it to run in the background so it is executed using sh -c.

#+name: webserver
#+begin_src shell
  sh -c 'python -m http.server 8001 -b 127.0.0.1 -d /tmp/ &>/dev/null & echo $!'
#+end_src

#+name: webserverpid
#+RESULTS: webserver
: 4080769

The result contain the pid of the python process.

The following can then be used to kill the web server:

#+begin_src shell :var webserverpid=webserverpid :results silent
kill $webserverpid
#+end_src

Your Answer

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

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