Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法杀死Ubuntu上Popen启动的Flask服务器_Python_Flask_Popen - Fatal编程技术网

Python 无法杀死Ubuntu上Popen启动的Flask服务器

Python 无法杀死Ubuntu上Popen启动的Flask服务器,python,flask,popen,Python,Flask,Popen,以下是我试图达到的目标的简化要求 # run.py import requests import time from subprocess import Popen, PIPE server = Popen("./app.py", stdout=PIPE, stderr=PIPE, shell=True) time.sleep(1) res = requests.get("http://localhost:1234/") assert res.status_code == 200 serve

以下是我试图达到的目标的简化要求

# run.py
import requests
import time

from subprocess import Popen, PIPE

server = Popen("./app.py", stdout=PIPE, stderr=PIPE, shell=True)
time.sleep(1)
res = requests.get("http://localhost:1234/")
assert res.status_code == 200
server.kill()
server.terminate()
res = requests.get("http://localhost:1234/")
print res
和实际的服务器脚本

#!/usr/bin/env python
from flask import Flask, make_response, request

app = Flask(__name__)
@app.route('/')
def view():
    return make_response("")

if __name__ == "__main__":
    app.run(host="localhost", port=1234)
在命令行上,我运行
python run.py
。来自壳牌:

(t)yeukhon@fubini:/tmp$ ps aux|grep app
yeukhon  21452  0.6  0.4  16416  9992 pts/2    S    03:50   0:00 python ./app.py
yeukhon  21471  0.0  0.0   4384   804 pts/2    S+   03:51   0:00 grep --color=auto app
因此,
app.py
仍然挂在那里。我必须从命令行杀了它。实际上,
run.py
的最后一行告诉我们服务器仍然处于活动状态(返回了200)

我尝试使用
os.kill(server.pid,signal.SIGTERM)
os.kill(server.pid,signal.SIGKILL)
进行杀戮,但都不起作用

正常情况下,
kill
可以工作,但我真的不确定为什么它不能接收信号。我肯定Flask是拒绝停止的

我有什么选择

奇怪的是,我上面的脚本在MacOSX上运行得非常好(我使用的是10.8.5,Mountain Lion)。到目前为止,我已经在两台Ubuntu12.04机器上进行了测试,它们都有相同的行为。我在Ubuntu机器上运行Python2.7.3,在MacOSX上运行Python2.7.2

更正
我唯一的选择就是使用。但我不想。是的,我必须使用Popen启动一个命令。

通过指定
shell=True
,命令由subshell运行

server = Popen("./app.py", stdout=PIPE, stderr=PIPE, shell=True)
server.terminate
将终止子shell,但不会终止web服务器



如何验证这一点?在调用
Popen
后尝试
print server.pid
,并将其与
ps
输出进行比较。

通过指定
shell=True
,命令由子shell运行

server = Popen("./app.py", stdout=PIPE, stderr=PIPE, shell=True)
server.terminate
将终止子shell,但不会终止web服务器



如何验证这一点?在调用
Popen
后尝试
print server.pid
,并将其与
ps
输出进行比较。

从Popen中删除
shell=True
。这将发出第一个请求。终止进程。然后为第二次尝试抛出异常。

从Popen中删除
shell=True
。这将发出第一个请求。终止进程。然后为第二次尝试抛出异常。

是。这很有效。我误解了shell的用法。但你说的验证线是什么意思?另外,为什么不能终止守护进程?非常感谢。@cppleener,
print server.pid
ps..grep..
的输出将不同。谢谢。你知道有什么好的资源可以帮助我更好地理解这一点吗?我不想再看我的操作系统手册了。很抱歉,非常感谢您的帮助。@CppLearner,很抱歉,非守护进程也不受影响。(至少在Linux中是这样)。从答案中删除该子句。@cplearner,请参见和Yes。这很有效。我误解了shell的用法。但你说的验证线是什么意思?另外,为什么不能终止守护进程?非常感谢。@cppleener,
print server.pid
ps..grep..
的输出将不同。谢谢。你知道有什么好的资源可以帮助我更好地理解这一点吗?我不想再看我的操作系统手册了。很抱歉,非常感谢您的帮助。@CppLearner,很抱歉,非守护进程也不受影响。(至少在Linux中是这样)。从答案中删除了这一条款。@CppLearner,请看,在我们讨论这一问题时,您知道有什么好地方可以更好地理解这一领域(我不想再看一遍我那本厚厚的操作系统教程)。对不起,谢谢。@CPP学习者明白什么?:)nvm。falsetru提供了一些。我对提高我对popen的使用率很感兴趣。在我们进行这项工作时,你知道有什么好地方可以更好地了解这一领域(我不想再看一遍我那本厚厚的操作系统教程)。对不起,谢谢。@CPP学习者明白什么?:)nvm。falsetru提供了一些。有兴趣提高我对popen的使用率。嗨,学习者。我遇到了同样的问题。然而,似乎我必须将shell=True设置为通过popen运行烧瓶。否则,我将收到类似“raisechild\u exception OSError:[Errno 2]没有这样的文件或目录”的错误消息。因此,我想知道你最终是如何修改代码的。当shell=True时,服务器从popen启动,请继续运行。您好。我遇到了同样的问题。然而,似乎我必须将shell=True设置为通过popen运行烧瓶。否则,我将收到类似“raisechild\u exception OSError:[Errno 2]没有这样的文件或目录”的错误消息。因此,我想知道你最终是如何修改代码的。当shell=True时,从popen启动的服务器将继续运行。