2017-10-05 205 views
1

這就是我想做的事:運行多個Python腳本同時

  1. 通話約8同時從Stata的Python腳本(以節省時間;如果順序執行,他們要花很長時間)。我知道該怎麼稱呼一個:

    殼 「C:/Python34/python.exe」 「A:/我的代碼/ Test.py」

  2. 有無Stata的等待,直到他們都完成,然後做的東西之內Stata的。

是否可以同時調用幾個Python腳本?

+2

可以的WinExec(而不是外殼)第一個K-1和第K個外殼。如果第k次比其他時間花費更少的時間,這將不起作用。否則,請看看-parallel-。 –

+0

我認爲這樣可以很好地工作,特別是因爲我可以選擇時間最長的一個,併爲緩衝區添加一個等待命令。謝謝! – RandomCat

+0

另一種選擇是編寫一個while循環來檢查python腳本的輸出,並在睡眠命令沒有全部存在時執行。 –

回答

2

我無法訪問Windows機器,但類似這樣的東西可能會訣竅。改變第二部分以匹配你正在用Python做什麼。

/* (1) Instead of Python scripts, count some files and store the counts */ 
winexec rm "/Users/dimitriy/*_count.txt" 
winexec find /Users/dimitriy -type f -name '*.ado' | wc -l > ado_count.txt 
winexec find /Users/dimitriy -type f -name '*.pdf' | wc -l > pdf_count.txt 
winexec find /Users/dimitriy -type f -name '*.do' | wc -l > do_count.txt 


/* (2) Wait for ALL 3 files to be generated since Stata does not wait for winexec commands to finish */ 
capture ssc install fs, replace 

while "`num_files'" != "3" { 
    local num_files: word count `r(files)' 
    sleep 10000 // sleep 10 seconds 
    fs *_count.txt 
} 

di "All Done!" 

迴應如下評論:

這是沒有意義的,不會的原因有一大堆工作。我假設你的Python腳本吐出8個輸出文件。由於我不知道它們是什麼,因此我嘗試使用3個Mac命令生成三個文件,以在步驟1中爲我提供一些幫助。步驟2在繼續之前驗證這3個文件是否存在。

假設每個Python腳本我產生一種名爲output_i.txt文件I = 1,...,8,你需要有這樣的事情:

forvalues v=1/8 { 
    winexec "C:/Python34/python.exe" "D:/my code/PythonCode`v'.py" 
} 

capture ssc install fs, replace 

while "`num_files'" != "8" { 
    local num_files: word count `r(files)' 
    sleep 10000 // sleep 10 seconds 
    fs output_*.txt 
} 
+0

'fs'這裏必須使用'ssc install fs'在代碼運行之前安裝。 –

+1

@NickCox有一天我會記住這一點!感謝您再次捕捉到這一點。 –

+0

如果用戶編寫的命令成爲其他用戶標準庫的一部分,則該命令成功。這是恭維。但它不能幫助用戶不知道它來自哪裏! –