运行多个Python脚本同时
这就是我想做的事:运行多个Python脚本同时
-
通话约8同时从Stata的Python脚本(以节省时间;如果顺序执行,他们要花很长时间)。我知道该怎么称呼一个:
壳 “C:/Python34/python.exe” “A:/我的代码/ Test.py”
有无Stata的等待,直到他们都完成,然后做的东西之内Stata的。
是否可以同时调用几个Python脚本?
我无法访问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
}
'fs'这里必须使用'ssc install fs'在代码运行之前安装。 –
@NickCox有一天我会记住这一点!感谢您再次捕捉到这一点。 –
如果用户编写的命令成为其他用户标准库的一部分,则该命令成功。这是恭维。但它不能帮助用户不知道它来自哪里! –
可以的WinExec(而不是外壳)第一个K-1和第K个外壳。如果第k次比其他时间花费更少的时间,这将不起作用。否则,请看看-parallel-。 –
我认为这样可以很好地工作,特别是因为我可以选择时间最长的一个,并为缓冲区添加一个等待命令。谢谢! – RandomCat
另一种选择是编写一个while循环来检查python脚本的输出,并在睡眠命令没有全部存在时执行。 –