【问题标题】:In python 2.7 using CGI, how to check whether forked process completed在使用 CGI 的 python 2.7 中,如何检查分叉进程是否完成
【发布时间】:2016-08-25 21:36:28
【问题描述】:

所以我有一组 python 脚本。为了制作一个简单的 GUI,我一直在结合 html 和 CGI​​。到目前为止,一切都很好。但是,我的一个脚本需要很长时间才能完成(> 2 小时)。很明显,当我在我的服务器(mac 上的本地主机)上运行它时,我得到一个“网关超时错误”。我正在阅读有关分叉子流程并检查该流程是否完成的信息。
这是我想出的,但它不起作用:(。

import  os, time
#upstream stuff happening as part of main script
pid=os.fork()
    if pid==0: 
        os.system("external_program") #this program will make "text.txt" as output
        exit()

while os.stat(""text.txt").st_size == 0: # check whether text.txt has been written, if not print "processing" and continue to wait
    print "processing"
    sys.stdout.flush()
    time.sleep(300)
#downstream stuff happening

一如既往,感谢任何帮助

【问题讨论】:

    标签: python subprocess cgi


    【解决方案1】:

    你试过这个吗:

    import os
    processing = len(os.popen('ps aux | grep yourscript.py').readlines()) > 2
    

    它会告诉您脚本是否仍在运行(返回布尔值)。

    【讨论】:

    • 在该主题中投反对票意味着您知道更好的解决方案,请分享,我将删除我的。让我们不要在没有好的答案的情况下离开 OP。谢谢。
    猜你喜欢
    • 2021-07-28
    • 2012-05-27
    • 2021-11-08
    • 2014-12-10
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多