Python 2.7 子进程 Popen returns None
Python 2.7 subprocess Popen returns None
我目前正在 python 2.7 中使用 pytest 进行一组集成测试,执行以下操作:
1) 运行 我本地机器后台的服务器二进制文件
2) 向服务器发送请求并验证结果
3) 终止后台服务器进程
一切似乎都运行良好,只是我无法终止我计算机上的服务器进程 运行ning。虽然它在我的电脑上继续 运行,但 Python 似乎已经忘记了它;我的 Popen
对象是 None
.
AttributeError: 'NoneType' object has no attribute 'terminate'
有没有想过是什么原因造成的?我是否遗漏了一些明显的东西?
import time
import subprocess
server_background_process_pipe = None
def setup_module():
# Start the test server in the background
cmd = 'bin/my_server --key1='+value1+' --key2='+value2+' &' # The '&' tells my bin to run in the background
server_background_process_pipe = subprocess.Popen(cmd, shell=True,stderr=subprocess.STDOUT)
print(server_background_process_pipe) # prints '<subprocess.Popen object at 0x10aabd250>'
time.sleep(1) # Wait for the server to be ready
def test_basic_get_request():
print(server_background_process_pipe) # prints 'None'
response = send_request_to_server()
fail_if_not_as_expected(response) # Response is exactly as expected
def teardown_module():
# kill the server that was launched in setup_module to serve requests in the tests
# AttributeError: 'NoneType' object has no attribute 'terminate'
server_background_process_pipe.terminate()
额外信息:
它是 None
,即使服务器进程仍在 运行ning。它是 None
,而测试是 运行ning。测试套件完成后 运行s。如果我重新 运行 测试,我会在控制台中收到一条消息,表明我的服务器部署失败,因为它已经 运行ning。测试仍然通过,因为它们从之前的执行中向服务器发送请求。
由于服务器需要在后台 运行,我直接使用 subprocess.Popen 构造函数而不是像 check_output
.
这样的便捷方法之一
在
def setup_module():
…
server_background_process_pipe = subprocess.Popen(…)
server_background_process_pipe
是局部变量。它从未分配给 global server_background_process_pipe
所以 global server_background_process_pipe
总是 None
而代码
def teardown_module():
server_background_process_pipe.terminate()
尝试从 None.
获取属性 terminate
你想要的是对全局变量进行初始赋值:
def setup_module():
…
global server_background_process_pipe
server_background_process_pipe = subprocess.Popen(…)
我目前正在 python 2.7 中使用 pytest 进行一组集成测试,执行以下操作:
1) 运行 我本地机器后台的服务器二进制文件
2) 向服务器发送请求并验证结果
3) 终止后台服务器进程
一切似乎都运行良好,只是我无法终止我计算机上的服务器进程 运行ning。虽然它在我的电脑上继续 运行,但 Python 似乎已经忘记了它;我的 Popen
对象是 None
.
AttributeError: 'NoneType' object has no attribute 'terminate'
有没有想过是什么原因造成的?我是否遗漏了一些明显的东西?
import time
import subprocess
server_background_process_pipe = None
def setup_module():
# Start the test server in the background
cmd = 'bin/my_server --key1='+value1+' --key2='+value2+' &' # The '&' tells my bin to run in the background
server_background_process_pipe = subprocess.Popen(cmd, shell=True,stderr=subprocess.STDOUT)
print(server_background_process_pipe) # prints '<subprocess.Popen object at 0x10aabd250>'
time.sleep(1) # Wait for the server to be ready
def test_basic_get_request():
print(server_background_process_pipe) # prints 'None'
response = send_request_to_server()
fail_if_not_as_expected(response) # Response is exactly as expected
def teardown_module():
# kill the server that was launched in setup_module to serve requests in the tests
# AttributeError: 'NoneType' object has no attribute 'terminate'
server_background_process_pipe.terminate()
额外信息:
它是 None
,即使服务器进程仍在 运行ning。它是 None
,而测试是 运行ning。测试套件完成后 运行s。如果我重新 运行 测试,我会在控制台中收到一条消息,表明我的服务器部署失败,因为它已经 运行ning。测试仍然通过,因为它们从之前的执行中向服务器发送请求。
由于服务器需要在后台 运行,我直接使用 subprocess.Popen 构造函数而不是像 check_output
.
在
def setup_module():
…
server_background_process_pipe = subprocess.Popen(…)
server_background_process_pipe
是局部变量。它从未分配给 global server_background_process_pipe
所以 global server_background_process_pipe
总是 None
而代码
def teardown_module():
server_background_process_pipe.terminate()
尝试从 None.
获取属性terminate
你想要的是对全局变量进行初始赋值:
def setup_module():
…
global server_background_process_pipe
server_background_process_pipe = subprocess.Popen(…)