如何使Mathematica内核暂停以创建外部文件

Pla*_*iac 10 wolfram-mathematica

是否有可能在计算过程中暂停Mathematica内核?这是一个例子.

Module[{},
       Mathematica code....
       ..........
       ..........
       {
        Calls an external program with some argument
        Needs to wait for an external program to create a file (* How ?*)
        }
       Mathematica code using that file content....
       ...........
       ...........
      ]
Run Code Online (Sandbox Code Playgroud)

我可以提出一个Do[..]循环解决方案,继续检查指定的目录是否创建了文件.一旦找到文件,它就会读取内容,Mathematica代码的其余部分会处理数据.

有没有优雅的方法来解决这个问题?

BR

Chr*_*nen 12

试试Pause[n],暂停至少n秒.

编辑:要使其在不确定的时间内工作,您需要重复轮询文件系统.FileExistsQ这样做,你就像使用它一样

While[!FileExistsQ[ "filename" ], Pause[1]]
Run Code Online (Sandbox Code Playgroud)

在等待时最多只有一秒的浪费时间.

进一步编辑:您还可以将文件存在轮询放在批处理文件中,从而释放您的Mathematica会话.例如,创建一个名为C:\ Temp\Test.bat的批处理文件,其中包含:

@echo off
start /min apame_win64 input
echo Loop commenced %TIME%
:loop
rem wait three seconds
ping localhost -n 3 > nul
if not exist c:\temp\alldone.txt goto loop
rem wait while file is completely written out
ping localhost -n 3 > nul
rem then terminate the process
taskkill /f /fi "imagename eq apame_win64.exe"
exit
Run Code Online (Sandbox Code Playgroud)

从Mathematica调用它: Run["start /min c:\\temp\\test.bat"]

这个批处理演示假设apame_win64将写出一个文件alldone.txt来完成.

  • 尝试:`当[!FileExistsQ ["filename"],暂停[1]]`在等待时最多浪费一秒钟. (7认同)

Mat*_*rth 6

您调用外部程序,创建文件后该程序是否退出?如果是这样,那么RunThrough就是您要查找的内容,请参阅RunThrough示例.在那里,他们使用Mathematica的另一个实例作为外部程序(比如执行Mathematica脚本并等待其结果).

如果在创建文件后外部程序必须继续运行,那么您可以使用单独的脚本(shell脚本,python,ruby ...)来检查文件是否存在.