脚本完成之前输出文件不包含任何内容 [英] Output file contains nothing before script finishing

查看:50
本文介绍了脚本完成之前输出文件不包含任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个python脚本,其中有几个 print 语句.打印的信息可以帮助我监视脚本的进度.但是当我qsub bash脚本时,该脚本包含 python my_script&>.输出到计算节点上,即使脚本正在运行并打印某些内容,输出文件也不包含任何内容.脚本完成后,输出文件将包含输出.因此,当脚本运行时,如何通过输出文件实时获取输出.

I write a python script in which there are several print statement. The printed information can help me to monitor the progress of the script. But when I qsub the bash script, which contains python my_script &> output, onto computing nodes, the output file contains nothing even when the script is running and printing something. The output file will contains the output when the script is done. So how can I get the output in real time through the output file when the script is running.

推荐答案

实际上是在每次写入后或每次写入调用后写入文件而不是管道并刷新 sys.stdout.flush()您最好使用记录器功能,并用日志替换打印内容.

Actually write to the file rather than piping and flush after each write or after each write call sys.stdout.flush() but you are better off using a logger function and replacing the prints with logs.

来自评论:记录器功能是您调用的一种功能,而不是打印功能,它将输出到文本的某处(可能带有时间戳)以及其他信息,它们通常使您可以将各种信息输出到各种目的地,包括标准输出和文件.请参阅python 2

From Comments: A logger function is one that you call instead of print that will output to somewhere the text, possibly timestamped and with other information, they usually let you output various amounts of information to various destinations including stdout and files. See python 2 or 3 documents for information on pythons built in logging function.

这篇关于脚本完成之前输出文件不包含任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆