问题描述
如果我处于需要依次执行外部命令的情况,那么最好的解决方案是什么?
例如,我有两个命令make snapshot 和备份快照第二个无法启动,直到第一个完成。
如果我有条件地将这两个命令粘在do语法中,它们会被一个接一个地执行还是必须手动检查并确保第一个命令完成?
对于手动完成检查,是否足够使用system或rawSystem并检查其退出代码? system和runCommand函数,有人可以向我澄清一下,我只能看到它们返回不同的值:退出代码和进程句柄。有没有其他的区别?
我是否需要使用runCommand来完成上述序列的工作?我是否需要在进程句柄上调用wait?
谢谢。
我知道您正在使用模块来运行外部命令。这很好。
模块包含阻塞和ñ阻止IO操作。非阻塞模块(例如 createProcess
, runCommand
)会创建一个进程并立即返回它的句柄, 。 (例如 readProcess
, system
)不会返回任何句柄,而是返回运行过程的结果一旦它终止。
为确保进程已终止,您需要使用阻止操作,或者使用 waitForProcess
,它会阻塞,直到具有给定句柄的进程死亡。
是。
主要区别在于 system
阻止,而 runCommand
不是。
不,阻止调用应该足够您的情况。
仅当您决定使用非阻塞呼叫时。
使用示例:
import System.Process
main = do
ExitSuccess< ; - systemmake snapshot
ExitSuccess< - systembackup snapshot
return()
If I am in a situation where I need to execute external commands in sequence, what is the best solution?
For instance, I have two commands "make snapshot" and "backup snapshot"The second cannot start till the first one is complete.If I orderly stick those two commands in a do syntax would they be executed one after another or do I have to manually check and make sure the first one is complete?
For the manual completion check, is it enough to use "system" or rawSystem" and examine their exit code?
I don't fully understand the difference between "system" and "runCommand" functions. Can someone clarify this to me. I can only see they return different values: exit code vs process handle. Any other differences?
Would I rather need to use "runCommand" for the above sequence to work? Do I need to call wait on the process handle?
Thanks.
I understand you are using the System.Process module to run the external commands. This is good.
The module contains both blocking and non-blocking IO actions. The non-blocking ones (like createProcess
, runCommand
) create a process and return its handle immediately, while it's still running. The blocking ones (like readProcess
, system
) do not return any handles, but rather return the result of running the process once it terminates.
To ensure that the process has terminated, you need to either use blocking actions, or use waitForProcess
, which blocks until the process with the given handle dies.
Yes.
The main difference is system
is blocking while runCommand
is not.
No, blocking calls should be enough in your case.
Only if you decide to use non-blocking calls.
Example of usage:
import System.Process
main = do
ExitSuccess <- system "make snapshot"
ExitSuccess <- system "backup snapshot"
return ()
这篇关于Haskell:按照严格的顺序执行外部命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!