【发布时间】:2020-02-14 10:40:33
【问题描述】:
我有一种情况,我想在 Azure 发布阶段连续启动两个 Python 看门狗,然后继续执行以下任务。
据我了解,subprocess.Popen 是您想要创建这样的“即开即忘”行为的最佳选择
当我在 Azure 之外运行 subprocess.Popen(["python", "mywatchdog1.py"]) 之类的东西时,它的行为符合我的预期,它“触发并忘记”,但是当从任务运行相同的调用时(尝试使用“Powershell”和“Run在 Azure Devops 中使用 Python 脚本”),任务停止并等待看门狗进程完成。
这是一个例子:
# mywatchdog1.py
import time
import os
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
class MyWatchdogEventHandler(LoggingEventHandler):
def dispatch(self, event):
print(f"A new dog was created")
if __name__ == "__main__":
dog_file_path = "c:\\dogs"
event_handler = MyWatchdogEventHandler()
observer = Observer()
observer.schedule(event_handler, dog_file_path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
这就是我尝试使用以下方式启动的内容:
import subprocess as sp
if __name__ == "__main__":
sp.Popen(["python", "C:\\scripts\\mywatchdog01.py"])
那么,无论您在本地运行还是在 Azure Windows 2016 代理上运行,我如何使 subprocess.Popen 的行为方式相同?任何帮助将不胜感激。
/尼克拉斯
【问题讨论】:
标签: python python-3.x azure-devops subprocess python-watchdog