为什么我的子进程STDOUT在nohup下不打印任何内容

2024-09-28 04:22:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个代码按以下方式执行

$ python mycode.py param1 param2 

它使用mycode.py内的subprocess生成输出:

import subprocess
paramlist = ['othercodes.py','infilel.txt']

p = subprocess.Popen(paramlist,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out, err = p.communicate()

# out is empty under nohup
print out

我可以使用以下命令获得完整的结果:

 $ python mycode.py  > output.txt 
 $ python mycode.py   >> output.txt 

但当我执行其中一个操作时,我在output.txt中得到空结果:

 $ nohup python mycode.py   > output.txt &
 $ nohup python mycode.py   >> output.txt &
 $ nohup python mycode.py   >> output.txt 2>&1  

正确的方法是什么?你知道吗

更新

我解决了这个问题。正如CharlesDuffy所指出的,问题出在othercodes.py。 尤其是这些线路:

        # # If there's input ready, do something, else do something
        # # else. Note timeout is zero so select won't block at all.

        # if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
        #     infile = sys.stdin.readline().strip()
        #     args.append(infile)

一旦我注释掉最后3行,mycode.py就可以了。你知道吗


Tags: pytxtoutputisstdinsysoutselect

热门问题