1
我正在使用子流程模塊啓動外部腳本,腳本運行從服務器發送/接收數據文件,並執行此操作爲〜10,000個不同的文件。該服務偶爾會掛起,如果重新啓動,可以繼續使用。外部腳本將文件(.xml)保存到一個文件夾中,所以當輸入文件的數量等於輸出文件夾中* .xml文件的數量時,該功能就完成了。這是我到目前爲止,第二個while循環似乎工作 - 這是監視如果沒有文件在文件夾中更新30分鐘,它應該終止進程。但是,在第一個while循環中,在進程終止之後,它不會重新啓動。任何幫助將是偉大的!Python啓動子流程腳本,監控結果,並在沒有活動的情況下重新啓動
from __future__ import division
import subprocess, datetime, time, os, glob
def runIPRscan(path, input, outputdir, email, num_complete):
num_files = len(glob.glob1(outputdir,"*.xml"))
while (num_files < num_complete):
#launch process
p = subprocess.Popen(['java', '-jar', path, '[email protected]', '-i', input, '-m', email, '-o', outputdir], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
time.sleep(180) #give the script a few minutes to get running
while p.poll is None:
#wait 30s and check again
time.sleep(30)
num_files = len(glob.glob1(outputdir,"*.xml"))
if num_files == num_complete:
break
#monitor the output folder for recent changes in last 30 minutes
now = datetime.datetime.now()
ago = now - datetime.timedelta(minutes=30) #if nothing happens in 30 minutes, relaunch service
file_list = []
for path in glob.glob(outputdir + "/*.xml"):
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(path)
if datetime.datetime.fromtimestamp(mtime) > ago:
file_list.append(path)
if not (file_list):
print("No activity in last 30 minutes, restarting service")
try:
p.terminate()
except OSError:
pass
time.sleep(10) #give it a few seconds to make sure process is closed before exiting
break
num_files = len(glob.glob1(outputdir,"*.xml"))
我想你是指'java'不是'javascript' – rbp
是的,對不起。用java編寫的外部腳本。我猜這與我的問題無關。 – jpalmer
你的問題是這個表達式'(num_files
rbp