【问题标题】:python subprocess.popen read only whats returnedpython subprocess.popen 只读返回什么
【发布时间】:2016-03-16 04:06:55
【问题描述】:

我对 python 很陌生。需要进一步了解 subprocess.popen。

我有一个执行另一个 python 脚本的脚本。下面是我的脚本将尝试执行另一个脚本的部分。

cmd = ['python %s %s %s %s %s'%(runscript, steps, part_number, serial_number, self.operation)]
p = subprocess.Popen(cmd, shell = True, stdout=subprocess.PIPE)
p.wait()
result = p.stdout.readline()

问题是,被执行的脚本,我必须打印出结果才能通过“result = p.stdout.readline()”读取结果。下面是被执行的脚本

def Main():

if sys.argv[1] == "Initiate" :
    doFunc = Functions_obj.Initiate()
    if doFunc != 0 :
        print doFunc
    else :
        print "Initiate PASS"

elif sys.argv[1] == "Check" :
    getDrive = Functions_obj.initialize()
    if getDrive == "NoDevice" :
        print getDrive
        sys.exit()

    doFunc = Functions_obj.Identify_Drive()
    if doFunc != 0 :
        print doFunc
    else :
        print "Check PASS"

我的问题是,我想从执行的脚本中“返回”结果而不是打印。我如何使用 subprocess.popen 做到这一点?以及如何使用子进程来获取返回的内容而不是打印的内容

【问题讨论】:

    标签: python


    【解决方案1】:

    您不能像函数调用那样通过管道返回数据。您必须使用许多 IPC 机制中的一种,例如管道、共享内存、消息传递或套接字。管道通常是最简单的,这就是您在这里所做的。不过,您可以通过管道发送二进制数据。您可以尝试腌制您的数据,假设它是可腌制的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 2016-12-05
      • 1970-01-01
      相关资源
      最近更新 更多