Common Lisp:使用与Lisp进程不同的工作目录启动子进程
问题内容:
假设我有一个目录A和一个子目录B。我进入A并启动了Lisp。在该Lisp进程中,我想启动一个Python子进程,其中Python将B视为其当前工作目录。lisp进程必须在A中具有cwd,而python进程应该在B中具有cwd。如何以跨平台的简单方式做到这一点?
我正在寻找一种适用于CCL和SBCL(可能使用“运行程序功能”)并且适用于Windows,Linux和OS X的解决方案。
我查看了CCL运行程序文档,但没有找到更改启动过程的cwd的方法。
我查看了Python命令行参数,但没有看到会改变python进程的cwd的参数。
我想到了对’cd B的运行程序调用;python …”,但我不确定它是如何工作的,因为它实际上在运行两个程序;cd,然后是python。
Python代码将作为输入(作为文件)提供,因此我无法更改任何代码(通过添加os.chdir()调用或类似代码)。
将python输入文件作为子进程启动的python包装器文件并不理想,因为我正在发送stdin并监听由lisp启动的python进程的stdout。在lisp和评估输入文件的python进程之间添加另一个子进程,这意味着我需要进行大量的stout
/ stdin中继,而且我感觉这很脆弱。
krzysz00的方法效果很好。由于目录更改是用lisp处理的,因此在启动python进程之前,此方法将适用于在不同子目录(不仅仅是python)中启动其他进程。
对于文档,这是我的代码,该代码使用了适用于SBCL和CCL的krzsz00的方法。请注意,它使用的是Hoyte的defmacro!宏,来自Let Over
Lambda
,可以轻松避免不必要的变量捕获:
#+:SBCL
(defun cwd (dir)
(sb-posix:chdir dir))
(defun getcwd ()
#+SBCL (sb-unix:posix-getcwd)
#+CCL (current-directory))
(defmacro! with-cwd (dir &body body)
`(let ((,g!cwd (getcwd)))
(unwind-protect (progn
(cwd ,dir)
,@body)
(cwd ,g!cwd))))
用法:
(with-cwd "./B"
(run-program ...))
问题答案:
要运行外部程序(如可移植的python进程),请参阅external-program。要更改当前的工作目录,请使用cwd
文件http://files.b9.com/lboot/utils.lisp中经过稍微修改的(公共领域)功能,该功能在下面复制。
(defun cwd (&optional dir)
"Change directory and set default pathname"
(cond
((not (null dir))
(when (and (typep dir 'logical-pathname)
(translate-logical-pathname dir))
(setq dir (translate-logical-pathname dir)))
(when (stringp dir)
(setq dir (parse-namestring dir)))
#+allegro (excl:chdir dir)
#+clisp (#+lisp=cl ext:cd #-lisp=cl lisp:cd dir)
#+(or cmu scl) (setf (ext:default-directory) dir)
#+cormanlisp (ccl:set-current-directory dir)
#+(and mcl (not openmcl)) (ccl:set-mac-default-directory dir)
#+openmcl (ccl:cwd dir)
#+gcl (si:chdir dir)
#+lispworks (hcl:change-directory dir)
#+sbcl (sb-posix:chdir dir)
(setq cl:*default-pathname-defaults* dir))
(t
(let ((dir
#+allegro (excl:current-directory)
#+clisp (#+lisp=cl ext:default-directory #-lisp=cl lisp:default-directory)
#+(or cmu scl) (ext:default-directory)
#+sbcl (sb-unix:posix-getcwd/)
#+cormanlisp (ccl:get-current-directory)
#+lispworks (hcl:get-working-directory)
#+mcl (ccl:mac-default-directory)
#-(or allegro clisp cmu scl cormanlisp mcl sbcl lispworks) (truename ".")))
(when (stringp dir)
(setq dir (parse-namestring dir)))
dir))))
结合这两个功能,所需的代码是:
(cwd #p"../b/")
(external-program:start "python" '("file.py") :output *pythins-stdout-stream* :input *pythons-stdin-stream*)
(cwd #p"../a/")
这将cd
到B,像运行by一样运行python进程python file.py &
,将python进程的stdin /
stdout发送到指定的流(external- program
有关更多详细信息,请参见文档),最后执行另一个cwd
将Lisp进程返回给A的操作。应该等到python进程完成后,使用external- program:run
代替external-program:start
。