睡在emacs lisp中

Bil*_*ong 17 lisp emacs sleep

脚本A.

    (insert (current-time-string))
    (sleep-for 5)
    (insert (current-time-string))
Run Code Online (Sandbox Code Playgroud)

M-x eval-buffer,插入两个时间串,相隔5秒

脚本B.

一些comint代码(添加钩子,启动进程)

    (sleep-for 60) ;delay a bit for process to finish
    (insert "ZZZ")
Run Code Online (Sandbox Code Playgroud)

M-x eval-buffer,"ZZZ"立即插入,没有任何时间延迟

可能发生了什么?顺便说一句,它是Win XP上的Emacs 23.2

Tho*_*mas 12

如果你想要做的就是等待一个过程完成,你根本不应该使用它sleep-for.而是同步调用进程,而不是异步调用:

http://www.gnu.org/software/emacs/manual/html_node/elisp/Synchronous-Processes.html#Synchronous-Processes

这样,Emacs将阻塞,直到该过程完成.

如果你必须(或者真的想)使用异步进程,例如因为它需要很长时间并且你不希望Emacs在那段时间内冻结(你说的是60秒,这很长),那么正确的方法等待进程完成是使用哨兵.sentinel是一个回调,只要进程状态发生变化(例如终止时),就会调用该回调.

(defun my-start-process ()
  "Returns a process object of an asynchronous process."
  ...)

(defun my-on-status-change (process status)
  "Callback that receives notice for every change of the `status' of `process'."
  (cond ((string= status "finished\n") (insert "ZZZ"))
        (t (do-something-else))))

;; run process with callback
(let ((process (my-start-process)))
  (when process
    (set-process-sentinel process 'my-on-status-change)))
Run Code Online (Sandbox Code Playgroud)


Eli*_*lay 11

它可能会中断睡眠以处理子进程IO.run-with-idle-timer因为Emacs是单线程的,所以你应该使用类似"延迟一点过程"的东西.