【问题标题】:Why Does Azure DevOps Server interleave output为什么 Azure DevOps 服务器交错输出
【发布时间】:2020-08-22 04:26:00
【问题描述】:

抱歉,由于工作中的安全限制,我无法发布实际代码,但我会尝试做一个人为的例子。

我正在使用 python 3.6.1 并在 Azure Pipeline (ADS 2019) 中运行一个模块。在模块中,我们使用具有以下结构的字典完成输出

#dummy data, assume files could be in any order in any category

{
    "compliant": ['file1.py', 'file2.py'], #list of files which pass
    "non-compliant":['file3.py'], #list of files which fail
    "incompatible":['file4.py'] #list of files which could not be tested due to exceptions
}

当发生故障时,我们的一位客户希望脚本输出命令以调用可以运行的脚本来纠正不合规的文件。该程序的编写类似于以下内容

result = some_func() #returns the above dict
print('compliant:')

for file in result['compliant']:
    print(file)

print('non-compliant:')
for file in result['non-compliant']:
    print(file)

print('incompatible:')
for file in result['incompatible']:
    print(file)

# prints a string to sys.stderr simillar to python -m script arg1 arg2 ...
# script that is output is based on the arguments used to call
print_command_to_fix(sys.argv) 

正常运行时,我会得到如下正确的输出:

#correct output: occurs on bash and cmd

compliant:
file1.py
file2.py
non-compliant:
file3.py
incompatible:
file4.py

python -m script arg1 arg2 arg_to_fix

当我在 Azure Pipeline 上运行时,输出会像下面这样交错

#incorrect output: occurs only on azure pipeline runs

compliant:
python -m script arg1 arg2  arg_to_fix
file1.py
file2.py
non-compliant:
file3.py
incompatible:
file4.py

无论我尝试使用 print 还是 sys.stderr.write 似乎都无法解决交错问题,并且我假设 print_command_to_fix() 以某种方式被异步调用。但我的猜测可能并不准确,因为我已经很久没有使用 ADS 或 python了。

TL;DR:我做错了什么只在管道上获得上述交错输出?

编辑:澄清某些点并修正错别字

【问题讨论】:

  • 你是如何配置你的构建定义的?您是否使用了最新的构建代理?请尝试在您的构建代理机器上手动运行代码以检查结果。此外,在构建管道中将 system.debug 设置为 True 并共享整个日志。

标签: python python-3.x azure output azure-devops-server-2019


【解决方案1】:

经过几个小时的故障排除和解决方案后发现了答案。

ADS 跟踪程序中的两个输出流,但它是异步进行的。该错误是由输出到 stdout 和 stderr 引起的。在这种情况下,将所有输出输出到一个流解决了这个问题。我采取的方法最终如下所示

result = some_func() #returns the above dict
output = []
output.append('compliant:')
output.extend(result['compliant'])

output.append(file)
output.extend(result['non-compliant'])

output.append('incompatible:')
output.extendresult['incompatible'])

# returns a string to simillar to python -m script arg1 arg2 ...
# script that is output is based on the arguments used to call
output.append(format_command_to_fix(sys.argv))
print('\n'.join(output))

另外,我想其他输出异步信息的技术也应该解决。

【讨论】:

  • 很高兴看到您的问题已得到解决。您可以Accept it as an Answer,这对阅读此主题的其他社区成员会有所帮助。
  • 谢谢你通知我,我以为它已经关闭但我想我忘记了。
猜你喜欢
  • 2021-02-18
  • 1970-01-01
  • 2021-08-01
  • 2022-07-18
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多