How can I make Mathematica kernel pause for an external file creation

£可爱£侵袭症+ 提交于 2019-12-05 01:34:13

Try Pause[n], pauses for at least n seconds.

Edit: to make it work for an indeterminate amount of time, you need to repeatedly poll the filesystem. FileExistsQ does this, and you'd use it like

While[!FileExistsQ[ "filename" ], Pause[1]]

which would at most have one second of wasted time while waiting.

Further Edit: You can also put the file existence poll in a batch file thereby freeing up your Mathematica session. E.g. make a batch file called C:\Temp\Test.bat containing:

@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

And call it from Mathematica: Run["start /min c:\\temp\\test.bat"]

This batch demo assumes apame_win64 will write out a file alldone.txt to complete.

You call an external program, does that program exit once the file is created? If so, then RunThrough is what you're looking for, see the RunThrough example. There they use another instance of Mathematica as the external program (like executing a Mathematica script and waiting for its result).

If the external program has to remain running after the file was created then you can use a separate script (shell script, python, ruby...) to check if the file exists.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!