如何终止作为服务运行的 Flask 应用程序? [英] How do I terminate a flask app that's running as a service?
问题描述
由于 是否可以在 Windows 中将 Python 脚本作为服务运行?如果可能的话,怎么做?,但是说到停止它我不能.我必须在任务管理器中终止进程.
I was able to get my flask app running as a service thanks to Is it possible to run a Python script as a service in Windows? If possible, how?, but when it comes to stopping it i cannot. I have to terminate the process in task manager.
这是我的 run.py,我通过 run.py install 把它变成了一个服务:
Here's my run.py which I turn into a service via run.py install:
from app import app
from multiprocessing import Process
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "CCApp"
_svc_display_name_ = "CC App"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None,0,0,None)
socket.setdefaulttimeout(60)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
server.terminate()
server.join()
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
self.main()
def main(self):
server = Process(app.run(host = '192.168.1.6'))
server.start()
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)
我从这篇文章中得到了过程的东西:http://librelist.com/browser/flask/2011/1/10/start-stop-flask/#a235e60dcaebaa1e134271e029f801fe 但不幸的是它也不起作用.
I got the process stuff from this post: http://librelist.com/browser/flask/2011/1/10/start-stop-flask/#a235e60dcaebaa1e134271e029f801fe but unfortunately it doesn't work either.
事件查看器中的日志文件说未定义全局变量服务器".但是,我已经将 server 设为全局变量,但它仍然给我同样的错误.
The log file in Event Viewer says that the global variable 'server' is not defined. However, i've made server a global variable and it still gives me the same error.
推荐答案
我推荐你使用 http://supervisord.org/.实际上不适用于 Windows,但使用 Cygwin,您可以像在 Linux 中一样运行 supervisor,包括作为服务运行.
I recommend you use http://supervisord.org/. Actually not work in Windows, but with Cygwin you can run supervisor as in Linux, including run as service.
安装 Supervisord:https://stackoverflow.com/a/18032347/3380763
For install Supervisord: https://stackoverflow.com/a/18032347/3380763
安装后你必须配置应用程序,这里是一个例子:http://flaviusim.com/blog/Deploying-Flask-with-nginx-uWSGI-and-Supervisor/(不需要你使用Nginx和Supervisor的配置就足够了)
After install you must configure the app, here an example: http://flaviusim.com/blog/Deploying-Flask-with-nginx-uWSGI-and-Supervisor/ (Is not necessary that you use Nginx with the Supervisor's configuration is enough)
这篇关于如何终止作为服务运行的 Flask 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!