本文介绍了如何在任务执行期间(实时)更新stdout.txt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Azure Batch执行具有不同步骤的过程,该过程是使用Python编程的.我想在Azure Batch的 stdout.txt 文件中记录我的过程所在的步骤.

I am using Azure Batch to execute a process which has different steps, the process is programmed in Python. I want to keep a log of the steps my process is in in the stdout.txt file in Azure Batch.

到目前为止,我正在打印我所处的步骤.完成批处理任务后,打印内容将显示在 stdout.txt 中.但是,我找不到实时更新 stdout.txt 文件的方法.

So far, I am printing the step I am in. With this, once the batch task is completed, the prints will appear in the stdout.txt. However, I couldn't find the way to update the stdout.txt file in real time.

例如,假设我有以下代码片段:

For instance, imagine I have the following code snippet:

print('Step 1')
# Step 1 code
print('Step 2')
# Step 2 code
print('Step 3')
# Step 3 code

执行完我的整个任务后, stdout.txt 将会如下所示:

Once my whole task is executed, the stdout.txt will look like this:

第2步

第3步

但是,在此过程中,如果我尝试访问该文件,它将为空.

However, during the process, if I try to access to the file, it will be empty.

我想知道是否有任何实时获取这些打印件的方法,而不必等到任务完成后才写下来,例如,当任务仍在步骤1中时,我的唯一一行 stdout.txt 为:

I am wondering if there is any way to get these prints in real time, not having to wait until the task is completed to have something written down, i.e. while the task is still in the Step 1, the only line in my stdout.txt would be:

文件将在新步骤开始时进行更新(并因此进行打印).有任何想法或提示吗?

And the file would be updated when a new step starts (and therefore the print). Any idea or hint?

推荐答案

这很可能是程序缓冲输出的副作用.如果需要立即在stdout流中查看数据,则可能需要刷新.您可以尝试在Python中刷新标准输出或启动Python处于非缓冲模式.

This is most likely a side effect of your program buffering output. If you need to see data in the stdout stream immediately, you will likely need to flush. You can attempt to flush stdout in Python or start Python in unbuffered mode.

这篇关于如何在任务执行期间(实时)更新stdout.txt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:25