2016-12-25 96 views
2

在Common Lisp後臺運行函數的最佳方式是什麼?具體而言,我撥打電話如 (trivial-shell:shell-command "<long and complicated command>"。這個操作阻塞了~10秒,但我不關心輸出,只是副作用 - 所以我希望它在後臺運行,以便程序流可以繼續。我試過在sb-thread:make-thread中包裝整個東西,但這似乎沒有什麼區別。Common Lisp:在後臺運行函數

如果可能的話,我會避免捲入各種複雜的線程。我在64位Gentoo Linux上運行SBCL 1.1.18。

+1

Trivial-shell是一個古老的[初級](http://www.cliki.net/trivial-shell)軟件包。你可以嘗試其他解決方案,例如[UIOP中的啓動程序](https://gitlab.common-lisp.net/asdf/asdf/tree/master/uiop) – Renzo

+1

「但這似乎沒有什麼區別。 」。你的SBCL支持線程安裝嗎?檢查':sb-thread'是否屬於'* features *'(或者簡單地評估REPL中的#+ sb-thread t')。如果它返回T,那麼你應該能夠啓動線程。 – coredump

回答

0

下面是對SBCL cl-asyncbordeaux-thread包的例子。假設你在當前目錄下有一個shell腳本./echo.sh。您可以在後臺運行腳本。調用腳本後,立即評估以下代碼,以便您在屏幕上獲得Waiting.....。腳本完成後,通知器被觸發並顯示Threaded job done.

請確保*features*包含SB-THREAD,正如@coredump所述。

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

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

如果你想捕捉shell腳本的標準輸出,加:output tsb-ext:run-program的說法。