Emacs 26 多进程问题

(make-thread (lambda () (call-process "/bin/zsh" nil '(:file "./outfile") nil "-c" "tail -f log")) "tail")

会导致Emacs 阻塞。虽然

(call-process "/bin/zsh" nil '(:file "./outfile") nil "-c" "tail -f log")

的确会让Emacs阻塞,但是为什么在分支进程运行会阻塞主进程?

1 个赞

不懂「threads」,感觉就算你创建了不止一个 threads,Emacs 在一个时刻只会运行一个,如果你运行了一个同步的 Process 堵塞了 Emacs,Emacs 也不会因此切换到别的 Thread。

另外,我试了下你的代码,然后 Emacs 就动不了了,只能强行杀掉重启。你可以用一个简单的例子实验:

(make-thread
 (lambda ()
   (call-process "sleep" nil nil nil "10")))

运行这段代码会让 Emacs 无法响应 10 秒钟,这 10 秒内 C-g 也无效。

emacs26的线程是有限制的, 一次只能一个在运行, 阻塞之前要让出控制权给其他线程, 而call-process是不会让出控制权的, 相当于独占模式. make-process这个才可以

2 个赞

好像是这样,我本来以为是我的问题。Manual上写

Concurrency in Emacs Lisp is “mostly cooperative”, meaning that Emacs will only switch execution between threads at well-defined times. However, the Emacs thread support has been designed in a way to later allow more fine-grained concurrency, and correct programs should not rely on cooperative threading.

意思是不是Emacs一次只能运行一个进程?

谢谢!make-process正是我需要的。晚上实验一下回来更新。