2011-10-05 18 views
10

在計算過程中是否可以暫停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.... 
     ........... 
     ........... 
     ] 

我可以拿出上在指定的目錄是否創建或不簽入文件保持一個Do[..]循環解決方案。一旦找到文件,它將讀取Mathematica代碼的內容和其餘部分處理數據。

有沒有什麼優雅的方法可以解決這個問題?

BR

+0

你在使用什麼操作系統? –

+0

@ belisarius Windows 7,但希望它可以在Linux或Mac上完成.. – PlatoManiac

回答

12

嘗試Pause[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 

而且從數學稱它爲:Run["start /min c:\\temp\\test.bat"]

這批演示假設apame_win64將寫出文件alldone.txt完成。

+0

我不知道多少時間停止..它可變。 – PlatoManiac

+7

嘗試:'while [!FileExistsQ [「filename」],Pause [1]]'這會在等待時浪費最多一秒。 – rcollyer

+1

@Chris,我將我的代碼添加到了您的答案中,因爲我認爲我的評論獲得的投票超過了您的答案,這有點不公平。 – rcollyer

6

你調用一個外部程序,程序是否會在文件創建完成後退出?如果是這樣,那麼RunThrough就是你要找的東西,請看RunThrough example。在那裏,他們使用另一個Mathematica實例作爲外部程序(如執行Mathematica腳本並等待其結果)。

如果外部程序在創建文件後必須保持運行,那麼您可以使用單獨的腳本(shell腳本,python,ruby ...)來檢查文件是否存在。

相關問題