我试图用cherrypy.engine.start 而不是cherrypy.quickstart 来运行cherrypy。那是因为我想在非阻塞状态下运行cherrypy,以使用py.test 在我的功能测试中启动和停止Web 服务器。
这工作正常:
cherrypy.quickstart(WebServerTest(None), config=testconf)
对 curl 的响应是:
curl --head http://127.0.0.1:1026/index HTTP/1.1 200 OK
Date: Thu, 08 Aug 2013 12:54:37 GMT
Content-Length: 0
Content-Type: text/html;charset=utf-8
Server: CherryPy/3.2.2
但它阻止了脚本的其余部分执行。
但是,这不起作用:
testconf = path.join(path.dirname(__file__), 'webservertest.conf')
web_server = WebServerTest(None)
cherrypy.tree.mount(web_server, "", config=testconf)
cherrypy.engine.start()
time.sleep(60)
cherrypy.engine.stop()
对 curl 的响应是:
curl --head http://127.0.0.1:1026/index
curl: (7) couldn't connect to host
在cherrypy.engine.start 之后添加cherrypy.engine.block() 并不能解决问题。
那么我怎样才能让它与cherrypy.engine.start()一起工作?
webservertest.conf 配置文件是:
[global]
server.socket_host = "127.0.0.1"
server.socket_port = 1026
server.thread_pool = 10
最佳答案
您还需要将 conf 传递给 cherrypy.config.update(conf)
。这是用于全局配置(包括您的服务器主机和端口),而 tree.mount 调用仅为该特定应用程序设置配置。阅读 quickstart
的源代码以查看所有血腥细节。
关于python - 非阻塞 CherryPy 没有收到任何信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18127415/