首页 > 解决方案 > 在 Azure Devops 发布管道中启动 Python 看门狗

问题描述

我有一种情况,我想在 Azure 发布阶段连续启动两个 Python 看门狗,然后继续执行以下任务。

据我了解,subprocess.Popen如果您想创建像这样的“触发并忘记”行为subprocess.Popen(["python", "mywatchdog1.py"])那么这是一条路从 Azure Devops 中的任务(尝试使用“Powershell”和“运行 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"])

那么,subprocess.Popen无论您是在本地运行还是在 Azure Windows 2016 代理上运行,我如何使行为方式相同?任何帮助将不胜感激。

/尼克拉斯

标签: pythonpython-3.xazure-devopssubprocesspython-watchdog

解决方案


那么,无论您是在本地运行还是在 Azure Windows 2016 代理上运行,如何使 subprocess.Popen 的行为方式相同?

抱歉,恐怕目前不支持这种情况。Azure Devops 管道会一一执行任务,在完成所有要在其中完成的工作之前,它不会完成当前任务。

这就是为什么它挂在你的PowershellRun a Python Script任务中的原因,因为watchdog它总是在听输入。您可以参考此文档了解更多详细信息。如果任务中有类似的东西watchdog保持监视文件资源管理器的变化,那么根据设计,任务将无法完成。

希望我的回答能解决你的疑惑:)


推荐阅读