【发布时间】:2014-09-09 09:42:37
【问题描述】:
我看到很多人都在为此苦苦挣扎——我也遇到了问题。如果有人可以帮助使 CherryPy 服务示例工作,那将是一个很大的帮助。解释这些问题将不胜感激。
有一个 CherryPy as a Windows Service 示例,位于:CherryPy Wiki。我正在努力让它发挥作用。这是我的代码:
"""
The most basic (working) CherryPy 3.0 Windows service possible.
Requires Mark Hammond's pywin32 package.
"""
import cherrypy
import win32serviceutil
import win32service
import win32event
import servicemanager
class HelloWorld:
""" Sample request handler class. """
def index(self):
return "Hello world!"
index.exposed = True
class MyService(win32serviceutil.ServiceFramework):
"""NT Service."""
_svc_name_ = "CherryPyService"
_svc_display_name_ = "CherryPy Service"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
# create an event that SvcDoRun can wait on and SvcStop
# can set.
self.stop_event = win32event.CreateEvent(None, 0, 0, None)
def SvcDoRun(self):
self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
cherrypy.tree.mount(HelloWorld(), '/')
# in practice, you will want to specify a value for
# log.error_file below or in your config file. If you
# use a config file, be sure to use an absolute path to
# it, as you can't be assured what path your service
# will run in.
cherrypy.config.update({
'global':{
'engine.autoreload.on': False,
'log.screen': False,
'log.error_file': 'c:\\MG\\temp\\CherryPy_Sample_Service.log',
'engine.SIGHUP': None,
'engine.SIGTERM': None
}
})
# set blocking=False so that start() does not block
cherrypy.server.quickstart()
cherrypy.engine.start(blocking=False)
# now, block until our event is set...
win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE)
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
cherrypy.server.stop()
win32event.SetEvent(self.stop_event)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(MyService)
以上内容与链接示例不同。我已经添加了
-
self.ReportServiceStatus(win32service.SERVICE_START_PENDING)和 -
self.ReportServiceStatus(win32service.SERVICE_RUNNING)作为SvcDoRun的第一个和最后一个语句 -
'log.error_file': 'c:\\MG\\temp\\CherryPy_Sample_Service.log',维基指示
重要提示:虽然可以使用控制台python cherrypy_sample_service.py install 安装服务,但不可能使用python cherrypy_sample_service.py start 命令启动它。这样做的原因是,以这种方式创建的服务将引用python 可执行文件,它不是作为服务设计的。
因此,为了进一步测试,我使用以下代码编译了代码:
from cx_Freeze import setup, Executable
exe = Executable(
script='cherrypy_sample_service.py'
)
build_options = {'includes': ['cherrypy', 'win32serviceutil', 'win32service', 'win32event', 'servicemanager']}
setup(
name = "CherryPy Sample Service",
version = "0.1",
service = ["cherrypy_sample_service.py"],
options = {"build_exe" : build_options},
executables = [exe])
在构建过程中,我收到以下警告:
Python27\App\lib\distutils\dist.py:267: UserWarning: Unknown distribution option: 'service'
warnings.warn(msg)
我已根据以下stack problem answer 添加了此选项。
现在我可以致电cherrypy_sample_service install、cherrypy_sample_service remove 和cherrypy_sample_service update。但是尝试运行服务(从服务或通过cherrypy_sample_service start)会导致以下错误:
Error starting service: The service did not respond to the start or control request in a timely fashion.
我被困住了。甚至没有创建日志文件。你能帮我让这个例子运行吗?我想如果我们可以解释并运行一个示例,这对于其他在相关问题上苦苦挣扎的人也很有用。谢谢!
【问题讨论】:
标签: python windows service cherrypy