简体   繁体   English

如何在PCF上托管的python CGI文件中刷新STDOUT?

[英]How to flush STDOUT in python CGI file hosted on PCF?

Due to Apache gateway timeouts, and a desire to display more information to the end user, I'd like to be able to flush STDOUT on a python CGI script hosted on PCF, essentially giving updates on the status of the script. 由于Apache网关超时,并且希望向最终用户显示更多信息,我希望能够在PCF上托管的python CGI脚本上刷新STDOUT,从本质上提供脚本状态的更新。

I have tried enabling the -u tag in python (#!/usr/python -u at head of my script), sys.stdout.flush() command, and even using subprocess.call to execute a perl script that is set to flush to STDOUT that prints some progress text ($| = 1; at beginning of perl script). 我尝试过在python中启用-u标记(在脚本开头为#!/ usr / python -u),sys.stdout.flush()命令,甚至使用subprocess.call来执行设置为刷新到STDOUT,该命令输出一些进度文本($ | = 1;在perl脚本的开头)。 Furthermore, I've double checked that I'm not using any Apache modules that would require buffering (no mod_deflate, for example). 此外,我再次检查了我是否没有使用任何需要缓冲的Apache模块(例如,没有mod_deflate)。 Finally, I'll mention that executing a standard perl CGI rather than a python CGI allows for the STDOUT flushing, so I figure it must be something with my python/Apache/PCF configuration. 最后,我将提到执行标准的perl CGI而不是python CGI可以进行STDOUT刷新,因此我认为它必须与python / Apache / PCF配置有关。

I'm fresh out of ideas here, and would like some advice. 我对这里的想法一无所知,并希望获得一些建议。

With any of these above methods, I would have thought stdout would flush. 使用以上任何一种方法,我都以为stdout会刷新。 But, none of them have worked! 但是,它们都没有起作用!

Thanks in advance for any assisstance. 在此先感谢您的协助。

You can disable buffering using something like this in your Python2 code: 您可以在Python2代码中使用类似以下的方式禁用缓冲:

    # set stdout as non-buffered
    if hasattr(sys.stdout, 'fileno'):
         fileno = sys.stdout.fileno()
         tmp_fd = os.dup(fileno)
         sys.stdout.close()
         os.dup2(tmp_fd, fileno)
         os.close(tmp_fd)
         sys.stdout = os.fdopen(fileno, "w", 0)

That is reopening sys.stdout with no buffer (ie the 0 as third arg). 那就是重新打开没有缓冲区的sys.stdout (即0作为第三个arg)。 After you do that, anything writing to sys.stdout should not be buffered. 执行sys.stdout不应缓冲写入sys.stdout任何内容。

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

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