【问题标题】:Common Lisp: Launch subprocess with different working directory than lisp processCommon Lisp:使用与 lisp 进程不同的工作目录启动子进程
【发布时间】:2012-04-06 21:14:57
【问题描述】:

假设我有一个目录 A 和子目录 B。我 cd 进入 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 包装文件并不理想,因为我正在发送标准输入并监听由 lisp 启动的 python 进程的标准输出。在 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 bash common-lisp dos ccl


    【解决方案1】:

    要运行外部程序(例如可移植的 python 进程),请参阅external-program。要更改当前工作目录,请使用文件http://files.b9.com/lboot/utils.lisp 中的这个稍作修改的(公共领域)函数cwd,复制如下。

    (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,像python file.py & 一样运行python 进程,将python 进程的stdin/stdout 发送到指定的流(查看external-program 文档了解更多详细信息),最后执行另一个cwd 将 lisp 进程返回给 A。如果 lisp 进程应该等到 python 进程完成,请使用 external-program:run 而不是 external-program:start

    【讨论】:

    • 不错的建议;谢谢;我没有使用外部程序兼容层b / c我只需要它用于sbcl和ccl,它们的运行程序基本相同,我无法轻松加载此代码的包,b / c它是未在服务器端使用。
    • 这里是否对多线程程序有任何影响,或者cwd 是否在每个线程的基础上更改?
    • 我认为cwd 是我对thread_safety(5) 的快速浏览中的每个线程,但我并不完全确定。看看它是否有效。
    【解决方案2】:

    我最终将 krzysz00 的建议写入了一个可以在 here 找到的包中。

    然后有人指出UIOP 带有getcwdchdir。如果你有一个相当新的 lisp,UIOP 应该包含在你的 asdf 版本中。

    【讨论】:

      【解决方案3】:

      我不知道 lisp 是什么,但这能行吗?

        import subprocess
        subprocess.Popen('python myscript.py', cwd='B')
      

      http://docs.python.org/library/subprocess.html

      【讨论】:

      • 我添加了一个额外的约束。在 lisp 和最终的 python 进程之间插入一个 python 包装进程并不理想;但也许我可以启动一个加载 python 包装文件的 python 进程;该文件将 cd,然后加载 python 输入文件?
      • 不得不说,这是一个很尴尬的问题。 cybertiggyr.com/gene/pathnames-0/node11.html
      • @Epic_orange:谢天谢地,有人编写了可移植层。
      • 问题是关于用普通的 lisp 进行操作
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      • 2018-04-09
      相关资源
      最近更新 更多