如何终止作为服务运行的烧瓶应用程序?
·
问题:如何终止作为服务运行的烧瓶应用程序?
感谢,我能够让我的烧瓶应用程序作为服务运行。是否可以在 Windows 中将 Python 脚本作为服务运行?如果可能,怎么做?,但是在停止它时我不能。我必须终止任务管理器中的进程。
这是我的 run.py,我通过 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但不幸的是它也不起作用。
事件查看器中的日志文件显示未定义全局变量“服务器”。但是,我已将 server 设为全局变量,但它仍然给我同样的错误。
解答
我推荐你使用http://supervisord.org/。实际上不适用于 Windows,但使用 Cygwin,您可以像在 Linux 中一样运行主管,包括作为服务运行。
对于安装 Supervisord:https://stackoverflow.com/a/18032347/3380763
安装后您必须配置应用程序,这里是一个示例:http://flaviusim.com/blog/Deploying-Flask-with-nginx-uWSGI-and-Supervisor/(您不必使用 Nginx 与 Supervisor 的配置就够了)
更多推荐
所有评论(0)