我正在描绘一组程序的架构,这些程序共享存储在数据库中的各种相互关联的对象.我希望其中一个程序充当服务,为这些对象的操作提供更高级别的接口,以及通过该服务访问对象的其他程序.
我目前的目标是将Python和Django框架作为实现该服务的技术.我很确定我想知道如何在Linux中守护Python程序.但是,它是系统应支持Windows的可选规范项.我对Windows编程没什么经验,也没有Windows服务的经验.
是否可以将Python程序作为Windows服务运行(即在没有用户登录的情况下自动运行)?我不一定要实现这一部分,但我需要大致了解如何做以决定是否按照这些方式进行设计.
编辑:感谢目前为止的所有答案,它们非常全面.我想知道一件事:Windows如何了解我的服务?我可以使用本机Windows实用程序进行管理吗? 在/etc/init.d中放置一个启动/停止脚本相当于什么?
我正在通过正常命令提示符下的python win_service.py install运行下面的代码,在那里我得到访问被拒绝错误.
安装服务TestService
安装服务时出错:访问被拒绝.(5)
当我以管理员身份启动时,我能够解决这个问题.
我能够安装该服务,但我无法启动该服务.
已安装服务
启动服务TestService
启动服务时出错:服务未及时响应启动或控制请求.
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "TestService"
_svc_display_name_ = "Test Service"
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):
print "running"
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)
Run Code Online (Sandbox Code Playgroud)
出了什么问题,有没有其他方法来安装解决问题的服务以及如何以管理员的身份动态运行它.