pythonsubprocess.call不在if州执行

2024-05-19 13:08:12 发布

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

我有一个街区:

if data[0] == OUTPUT:
    pin,val = ord(data[0]),ord(data[1])
    if (pin == 1): #Turn Master Bedroom Light on
        process = subprocess.call(cmd1, stdout=subprocess.PIPE)
        print process
    elif (pin == 2): #Turn Master Bedroom Light off
        process = subprocess.call(cmd2, stdout=subprocess.PIPE)
        print process
    elif (pin == 3 or pin == 4): #Toggle garage door
        process = subprocess.call(cmd3, stdout=subprocess.PIPE)
        print process
    else:
        print "Invalid Pin"

其中,cmd1、cmd2和cmd3是我尝试执行的shell脚本文件,输出是一个预定义的常量,等于1。我知道块实际上执行了,但它挂起了subprocess.call声明。在

当我跑步时:

^{pr2}$

在python解释器中,它本身工作得很好,但是在我的if elif块中,它会挂起。知道为什么吗?我完全困惑。。。我暂时忽略了val的价值,直到我能让它发挥作用。在

提前谢谢!我对python还不熟悉,所以请温柔点:)


Tags: masterdataifstdoutpinvalcallprocess
2条回答

documentation for subprocess.call状态

Do not use stdout=PIPE or stderr=PIPE with this function. As the pipes are not being read in the current process, the child process may block if it generates enough output to a pipe to fill up the OS pipe buffer.

OUTPUT is a predefined constant equaling 1

如果data[0] == 1,您的ord()调用将失败。你没提那件事,所以没发生类似的事。在

如果,如我所想,data可能是一个字符串或bytes()或unicode事物和data[0] == '\x01',那么有两种可能的情况:

要么OUTPUT真的是== 1,那么data[0] == OUTPUT的测试失败,就像'\x01' != 1,整个过程都被跳过了。这是最有可能的情况。在

或者OUTPUT == '\x01',然后执行整个东西,并执行第一个if块,但你告诉我们这不会发生。在

一个SSCCE会很有帮助。在

相关问题 更多 >