如何使 Mathematica 内核暂停以创建外部文件 [英] How can I make Mathematica kernel pause for an external file creation

查看:36
本文介绍了如何使 Mathematica 内核暂停以创建外部文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to pause Mathematica kernel during a computation? Here is an example.

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 代码会处理数据.

I can come up with a Do[..] loop solution that keeps on checking in a specified directory whether a file is created or not. Once it finds the file it reads the content and rest of the Mathematica code processes the data.

有没有什么优雅的方法可以解决这个问题?

Is there any elegant way to solve this problem?

BR

推荐答案

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

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

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

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.

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

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

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

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

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

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

这篇关于如何使 Mathematica 内核暂停以创建外部文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆