Question: Common Lisp: Run function in the background


Common Lisp: Run function in the background

Answers 2
Added at 2016-12-25 07:12

What's the best way to run a function in the background, in Common Lisp? Specifically, I'm making a call like (trivial-shell:shell-command "<long and complicated command>". This operation is blocking for ~10 seconds, but I don't care for the output, just the side effect - so I want it to be run in the background, so that program flow can continue. I've tried wrapping the whole thing in sb-thread:make-thread, but that didn't appear to make a difference.

I'd avoid getting wrapped up in all kinds of complicated threading, if at all possible. I'm running SBCL 1.1.18 on 64-bit Gentoo Linux.

nr: #1 dodano: 2016-12-26 22:12

My little investigation: it looks like the only solution is Renzo's answer: the launch-program function of UIOP.

Otherwise in order to run shell commands there is

nr: #2 dodano: 2016-12-27 14:12

Here is the example with cl-async and bordeaux-thread packages on SBCL. Suppose you have a shell script ./ at the current directory. You can run the script at the background. After the invocation of the script, the following code is immediately evaluated so you get Waiting..... on your screen. After the script is done, the notifier is triggered and displays Threaded job done.

Make sure the *features* contains SB-THREAD as @coredump says.

(require 'cl-async)
(require 'bordeaux-threads)

  (let ((notifier (as:make-notifier
                    (lambda ()
                       (format t "Threaded job done.~%")
    (format t "App started.~%")
    (bt:make-thread (lambda ()
                       (sb-ext:run-program "/bin/bash" (list "./"))
                       (as:trigger-notifier notifier))))
  (format t "Waiting......~%"))

If you want to capture the stdout of shell script, add :output t to the argument of sb-ext:run-program.

Source Show
◀ Wstecz