【发布时间】:2021-04-10 05:25:49
【问题描述】:
所以基本上我有一个脚本,可以从我的网络服务器下载 python 文件然后运行它们。我现在正在寻找一种方法来检测下载的 python 脚本是否已完成运行,因为我希望原始 python 脚本再次运行以下载下一个文件等等。我试图在没有用户输入的情况下完全自动化地做到这一点。我试图通过检查文本文件是否已更改来做到这一点,但我意识到这将要求任何尝试上传其代码的用户在其代码中添加文件更改功能。
这是我的一些代码供参考:
import time
import wget
import os
import requests
def lookForFile():
oldNumber=len(os.listdir("./upload"))
while True:
time.sleep(20)
NumberOfFiles = len(os.listdir("./upload"))
if NumberOfFiles != oldNumber:
os.system('python ./upload/1.py')
oldNumber = NumberOfFiles
def isRunning():
file = "save.txt"
with open(file) as f:
lines = f.readlines()
print(lines)
def notRunning():
f = open("save.txt", "w")
f.write("NotRunning")
f.close()
# open and read the file after the appending:
f = open("save.txt", "r")
print(f.read())
def updateRobot():
url = "url/1.py"
response = requests.get(url)
angel = response.json()
print(angel["currentNumber"])
def downloadScript():
url = "url/1.py"
wget.download(url, './upload/')
def main():
pass
【问题讨论】:
-
您使用的是哪个操作系统?您可以使用 pgrep -lf
查看它是否正在运行。或者使用 subprocess.run运行你的脚本,这将阻塞直到它完成,而不是 os.system() -
我目前正在 Windows pc 上测试它,但计划在我的树莓派上运行它
-
其实 os.system 也会阻塞。那么你的问题是什么?
标签: python