简体   繁体   English

如何确保脚本仅在subprocess.Popen完成执行之后才执行后续方法

[英]How to ensure that script only executes subsequent methods after subprocess.Popen has completed execution

I'm running into some problems here trying to use subprocess.Popen. 我在尝试使用subprocess.Popen时遇到了一些问题。 I cannot use subprocess.Call for the script I'm writing because I'd like to be able to specify the environment for my execution, something which I can only do so in Popen through an argument to the method. 我不能使用subprocess.Call来编写脚本,因为我希望能够为执行指定环境,这只能通过Popen方法的参数来实现。 I've noticed that subprocess.Popen takes longer to complete that subprocess.Call, and this creates problems downstream in my script because the rest of my script relies on the exit code (return code) from the spawned process to decide (through a set of conditional If statements) on a suitable action. 我注意到subprocess.Popen需要更长的时间来完成subprocess.Call,这会在脚本下游产生问题,因为脚本的其余部分依赖于生成的进程的退出代码(返回代码)来决定(通过一组)有条件的If语句)。

The method that involves using Subprocess is: 涉及使用子过程的方法是:

def execute_local(self):
'''
Spawns a test execution process locally and directs the standard output and 
error streams from the process to the appropriate log files.  
'''
self.return_code = subprocess.Popen(args = self.cmd_string, 
                                   stdout = open(self.out_log_filepath, 'w'),
                                   stderr = open(self.err_log_filepath, 'w'), 
                                   shell = True)

I have not yet specified the env arguments yet because I need to make sure this works before I can move on. 我还没有指定env参数,因为我需要先确保它能继续工作。

And the subsequent method that contains the conditionals is: 包含条件的后续方法是:

def get_status(self):
'''
Returns a string named either passed or failed back to itself, depending on 
exit status of the process that was spawned in the execute_local method.

This is important for main.py to be able to examine the status of the test 
processes and take appropriate administrative actions to either continue to 
a new test step or retry a give test.
'''
print self.return_code
if self.return_code == 0:
  return 'passed'
else:
  return 'failed'

In a higher level module, the methods would be called in the following order: execute_local ---followed by ----> get_status. 在更高级别的模块中,将按以下顺序调用这些方法:execute_local ---其次是----> get_status。

Previously when I did this with call, the execution went smoothly through to the conditionals, but now with Popen, it doesn't. 以前,当我使用call进行此操作时,执行过程顺利进行到了条件语句,但是现在使用Popen却没有。 When I tried debugging with a print statement to print out the return code of the process spawned by subprocess (I added a print self.return_code statement at the get_status method as you can see below), I've observed that with call, I actual get to see the return code, but with Popen, all I get is the object itself and its memory address. 当我尝试使用print语句进行调试以打印出子进程产生的流程的返回代码时(我在get_status方法中添加了一条print self.return_code语句,如下所示),我发现使用call时,实际上看到返回码,但是使用Popen,我得到的只是对象本身及其内存地址。

I would greatly appreciate any help on this, and also if someone could explain to me why Popen takes so much longer to run compared to call. 我将不胜感激,也可以有人向我解释为什么Popen与call相比需要花费更长的时间,对此我将不胜感激。

Thank you very much! 非常感谢你!

subprocess.call is just a wrapper around Popen that waits for the called process to exit. subprocess.call只是Popen的包装,它等待被调用的进程退出。 It takes the same arguments as Popen and could could just replace the (incorrectly implemented) Popen and replace it with call to get what you want. 它采用与Popen相同的参数,并且可以仅替换(未正确实现的) Popen ,并用call替换它以获得所需的内容。

self.return_code = subprocess.call(args = self.cmd_string, 
                                   stdout = open(self.out_log_filepath, 'w'),
                                   stderr = open(self.err_log_filepath, 'w'), 
                                   shell = True,
                                   env=my_env)

In your current implementation, you are returning the Popen object, not the return code. 在当前的实现中,您将返回Popen对象,而不是返回代码。

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

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