简体   繁体   English

为什么 Azure DevOps 服务器交错 output

[英]Why Does Azure DevOps Server interleave output

Sorry in advance, I can't post actual code because of security restrictions at my job, but I'll try to make a contrived example.提前抱歉,由于工作中的安全限制,我无法发布实际代码,但我会尝试做一个人为的例子。

I am working with python 3.6.1 and running a module in an Azure Pipeline (ADS 2019).我正在使用 python 3.6.1 并在 Azure 管道(ADS 2019)中运行一个模块。 In the module we have output done using a dictionary with the following structure在模块中,我们使用具有以下结构的字典完成 output

#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
}

When a failure occurs one of our customers wants the script to output the command to call a script that can be run to correct the non-compliant files.当发生故障时,我们的一位客户希望脚本 output 命令调用可以运行的脚本来纠正不符合要求的文件。 The program is written similar to what follows该程序的编写类似于以下内容

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) 

When run normally I would get the correct output like follows:正常运行时,我会得到正确的 output ,如下所示:

#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

when I run on the Azure Pipeline though, the output gets interleaved like follows当我在 Azure 管道上运行时,output 会像下面这样交错

#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

Whether I try to use print or sys.stderr.write it doesn't seem to resolve the interleave, and I'm assuming the print_command_to_fix() is being called asynchronously somehow.无论我尝试使用 print 还是 sys.stderr.write 它似乎都无法解决交错,并且我假设 print_command_to_fix() 以某种方式被异步调用。 But my guess probably isn't accurate since I haven't been working with ADS or python for very long.但我的猜测可能并不准确,因为我很久没有使用 ADS 或 python 了。

TL;DR: What am I doing wrong to get the above interleaved output on Pipelines only? TL;DR:我做错了什么才能在管道上获得上述交错 output?

Edit: clarified certain points and fixed typos编辑:澄清某些观点并修正错别字

Discovered the answer after a few hours of troubleshooting and solutions.经过几个小时的故障排除和解决方案后发现了答案。

ADS tracks both output streams in the program but does it asynchronously. ADS 跟踪程序中的两个 output 流,但它是异步进行的。 The error was cause by outputting to both stdout and stderr.该错误是由输出到 stdout 和 stderr 引起的。 This being the case, outputting all output to one stream resolved the issue.在这种情况下,将所有 output 输出到一个 stream 解决了该问题。 The approach I took ended up being something like follows我采取的方法最终如下

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))

Alternatively, I imagine other techniques for outputting async information should resolve as well.或者,我想其他输出异步信息的技术也应该解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM