停止执行多个文件 - Python
Stop Execution for Multiple Files - Python
我正在尝试通过 Windows 终端停止执行脚本 (run_all.py),该脚本 运行 包含在列表中的多个文件,具体取决于满足的特定条件在每个文件中。具体来说,在 file_2.py 中,我想根据“y”参数的值暂停该文件的执行,然后停止执行文件列表中尚未 运行 的任何文件(即file_3.py)。我目前正在使用时间模块暂停 file_2.py 60 秒,但我需要一种更优雅的方式来停止执行而不需要手动键盘命令(例如 Ctrl + C)...任何帮助都是最重要的赞赏!
file_1.py
print("Math is fun!")
file_2.py
import os
import time
x=1
y=2
if x < 10:
print("x is small")
else:
print("x is invalid")
if y < 10:
print("y is also small")
else:
print('press "ctrl + c" to stop execution of the workflow')
time.sleep(60)
file_3.py
print("python is so cool!")
run_all.py(来自 Windows 终端的主脚本 运行)
import os
path = "C:\users\mdl518\Desktop\"
file_list = ['file_1.py', 'file_2.py', 'file_3.py']
for filename in file_list:
os.system('python' + ' ' + os.path.join(path,filename)) # runs all files in the file_list
如果条件在其他文件之一中(如您的情况),请提出 ValueError
。例如:
x = 2
y = 9
if x < 10:
raise ValueError("x is small")
if y < 10:
raise ValueError("y is small")
最好将这些文件中的代码转换为函数并将函数导入 run_all
文件,而不是将它们作为单独的进程调用 python 到 运行 .例如:
文件 1 (f1.py):
def f1():
print("Math is fun!")
文件 2 (f2.py):
def f2(x, y):
if x < 10:
raise ValueError("y is small")
if y < 10:
raise ValueError("y is small")
文件 3 (f3.py):
def f3():
print("python is so cool!")
run_all 文件:
from f1 import f1
from f2 import f2
from f3 import f3
funs = [f1(), f2(1, 2), f3()]
for f in funs:
f
我正在尝试通过 Windows 终端停止执行脚本 (run_all.py),该脚本 运行 包含在列表中的多个文件,具体取决于满足的特定条件在每个文件中。具体来说,在 file_2.py 中,我想根据“y”参数的值暂停该文件的执行,然后停止执行文件列表中尚未 运行 的任何文件(即file_3.py)。我目前正在使用时间模块暂停 file_2.py 60 秒,但我需要一种更优雅的方式来停止执行而不需要手动键盘命令(例如 Ctrl + C)...任何帮助都是最重要的赞赏!
file_1.py
print("Math is fun!")
file_2.py
import os
import time
x=1
y=2
if x < 10:
print("x is small")
else:
print("x is invalid")
if y < 10:
print("y is also small")
else:
print('press "ctrl + c" to stop execution of the workflow')
time.sleep(60)
file_3.py
print("python is so cool!")
run_all.py(来自 Windows 终端的主脚本 运行)
import os
path = "C:\users\mdl518\Desktop\"
file_list = ['file_1.py', 'file_2.py', 'file_3.py']
for filename in file_list:
os.system('python' + ' ' + os.path.join(path,filename)) # runs all files in the file_list
如果条件在其他文件之一中(如您的情况),请提出 ValueError
。例如:
x = 2
y = 9
if x < 10:
raise ValueError("x is small")
if y < 10:
raise ValueError("y is small")
最好将这些文件中的代码转换为函数并将函数导入 run_all
文件,而不是将它们作为单独的进程调用 python 到 运行 .例如:
文件 1 (f1.py):
def f1():
print("Math is fun!")
文件 2 (f2.py):
def f2(x, y):
if x < 10:
raise ValueError("y is small")
if y < 10:
raise ValueError("y is small")
文件 3 (f3.py):
def f3():
print("python is so cool!")
run_all 文件:
from f1 import f1
from f2 import f2
from f3 import f3
funs = [f1(), f2(1, 2), f3()]
for f in funs:
f