我使用这里找到模板:Is it possible to run a Python script as a service in Windows? If possible, how?部署瓶的应用程序作为Windows服务
这里是我的run.py,我已经安装了如下在上面的链接指令的服务。
from app import app
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "Flask App"
_svc_display_name_ = "Flask 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)
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
self.main()
def main(self):
app.run(host = '192.168.1.6')
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)
然而,当我尝试启动服务,我得到的消息:
“本地计算机上的瓶应用服务启动,然后停止 一些服务自动停止,如果他们不使用的。其他服务或程序“。
任何想法我做错了什么?我尝试了各种用户帐户 - 我不认为这是一个权限问题。
谢谢!
感谢您的代码示例!我已经成功地使用它来在Windows服务器上_deploy_。然而,我得到**内部服务器错误**,只能用'os.chdir(os.path.dirname(os.path.realpath(__ file__))解决)) ' - 问题在于以下内容,脚本是从一些'win32'文件夹(作为服务)运行的,所以加载任何文件(预计在脚本的'dir'中)都会导致错误。 – quapka