Wolfram mathematica 如何使Mathematica内核暂停以创建外部文件
在计算过程中可以暂停内核吗?这里有一个例子Wolfram mathematica 如何使Mathematica内核暂停以创建外部文件,wolfram-mathematica,Wolfram Mathematica,在计算过程中可以暂停内核吗?这里有一个例子 Module[{}, Mathematica code.... .......... .......... { Calls an external program with some argument Needs to wait for an external program to create a file (* How ?*) }
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....
...........
...........
]
我可以想出一个Do[…]
循环解决方案,它可以在指定的目录中不断检查是否创建了文件。一旦找到文件,它将读取内容,Mathematica代码的其余部分将处理数据
有什么优雅的方法可以解决这个问题吗
BR如果调用外部程序,该程序是否会在创建文件后退出?如果是这样的话,那么您需要的就是RunThrough,请参阅。在那里,他们使用Mathematica的另一个实例作为外部程序(比如执行Mathematica脚本并等待其结果)
如果创建文件后外部程序必须保持运行,则可以使用单独的脚本(shell脚本、python、ruby…)检查文件是否存在。尝试
暂停[n]
,暂停至少n秒
编辑:要使其在不确定的时间内工作,需要反复轮询文件系统FileExistsQ
执行此操作,您可以像这样使用它
While[!FileExistsQ[ "filename" ], Pause[1]]
这最多会浪费一秒钟的等待时间
进一步编辑:您还可以将文件存在轮询放在批处理文件中,从而释放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
并从Mathematica调用它:Run[“start/min c:\\temp\\test.bat”]
此批处理演示假定apame_win64将写出一个文件alldone.txt来完成。@belisarius Windows 7,但希望它也能在Linux或Mac上完成。.我不知道要停止多少时间。.它的变量。请尝试:
而[!FileExistsQ[“filename”],暂停[1]
,这将在等待时最多浪费一秒钟。@Chris,我在你的答案中添加了我的代码,因为我认为我的评论比你的答案更受欢迎有点不公平。