Python 3.x 如何在没有传入连接的情况下在指定的时间内关闭CherryPy?
我正在使用CherryPy与身份验证服务器对话。如果所有输入的信息都正常,脚本运行正常。但是,如果他们输入ID时出错,内部HTTP错误屏幕将触发ok,但服务器将继续运行,脚本中的任何其他内容都不会运行,直到CherryPy引擎关闭,因此我必须手动终止脚本。是否有一些代码可以放在索引中Python 3.x 如何在没有传入连接的情况下在指定的时间内关闭CherryPy?,python-3.x,cherrypy,Python 3.x,Cherrypy,我正在使用CherryPy与身份验证服务器对话。如果所有输入的信息都正常,脚本运行正常。但是,如果他们输入ID时出错,内部HTTP错误屏幕将触发ok,但服务器将继续运行,脚本中的任何其他内容都不会运行,直到CherryPy引擎关闭,因此我必须手动终止脚本。是否有一些代码可以放在索引中 if timer >10 and connections == 0: close cherrypy (< I have a method for this already) 如果计时器>10且
if timer >10 and connections == 0:
close cherrypy (< I have a method for this already)
如果计时器>10且连接==0:
关闭cherrypy(<我已经有了一个方法)
我主要是一个数据管理员,所以不习惯网络服务器。当有太多的连接时,谷歌显示关闭CherryPy的点击率下降,但在指定(短)时间内没有连接时,则不会显示。我意识到web服务器的意义通常是等待连接,所以这可能是一种奇怪的情况。尽管如此,欢迎任何帮助。有趣的用例,您可以使用CherryPy插件基础架构来做类似的事情,看看这个
ActivityMonitor
插件实现,如果在指定的时间内(在本例中为10秒)没有处理任何事情并且没有看到任何请求,它将关闭服务器
也许您必须调整逻辑,了解如何关闭它,或者在\u verify
方法中执行其他操作
如果您想了解更多关于发布/订阅体系结构的信息,请参阅
导入时间
导入线程
进口樱桃
从cherrypy.process.plugins导入监视器
类活动监视器(监视器):
定义初始化(自身、总线、等待时间、监视器时间=无):
"""
巴士:cherrypy.engine
WaITiTimes:自上次请求以来我们认为是活动的秒。
监视器时间:在验证活动之前等待的秒数。
如果未定义,请等待“等待时间”的一半。
"""
如果监视器时间为“无”:
#如果未定义监控时间,则验证一半
#自上次请求以来的等待时间
监视器时间=等待时间/2
超级()。\uuu初始化__(
总线,自我验证,监控时间,自我类,自我名称__
)
#使用锁确保触发before\u请求的线程
#请求后不会与监视器方法冲突(\u验证)
self.\u active\u request\u lock=threading.lock()
self.\u活动\u请求=0
self.\u wait\u time=等待时间
self.\u last\u request\u ts=time.time()
def_验证(自):
#验证我们没有任何活动请求,并且
#如果我们没有看到任何活动,请关闭服务器
#自self.\u上次请求\u ts+self.\u等待\u时间
自激活请求锁定时:
如果(非自激活)请求和
self.\u上次请求\u ts+self.\u等待\u time
有趣的用例,你可以使用CherryPy插件基础架构来做类似的事情,看看这个ActivityMonitor
插件实现,如果在指定的时间内(在本例中为10秒)没有处理任何事情并且没有看到任何请求,它将关闭服务器
也许您必须调整逻辑,了解如何关闭它,或者在\u verify
方法中执行其他操作
如果您想了解更多关于发布/订阅体系结构的信息,请参阅
导入时间
导入线程
进口樱桃
从cherrypy.process.plugins导入监视器
类活动监视器(监视器):
定义初始化(自身、总线、等待时间、监视器时间=无):
"""
巴士:cherrypy.engine
WaITiTimes:自上次请求以来我们认为是活动的秒。
监视器时间:在验证活动之前等待的秒数。
如果未定义,请等待“等待时间”的一半。
"""
如果监视器时间为“无”:
#如果未定义监控时间,则验证一半
#自上次请求以来的等待时间
监视器时间=等待时间/2
超级()。\uuu初始化__(
总线,自我验证,监控时间,自我类,自我名称__
)
#使用锁确保触发before\u请求的线程
#请求后不会与监视器方法冲突(\u验证)
self.\u active\u request\u lock=threading.lock()
self.\u活动\u请求=0
self.\u wait\u time=等待时间
self.\u last\u request\u ts=time.time()
def_验证(自):
#验证我们没有任何活动请求,并且
#如果我们没有看到任何活动,请关闭服务器
#自self.\u上次请求\u ts+self.\u等待\u时间
自激活请求锁定时:
如果(非自激活)请求和
self.\u上次请求\u ts+self.\u等待\u timeimport time
import threading
import cherrypy
from cherrypy.process.plugins import Monitor
class ActivityMonitor(Monitor):
def __init__(self, bus, wait_time, monitor_time=None):
"""
bus: cherrypy.engine
wait_time: Seconds since last request that we consider to be active.
monitor_time: Seconds that we'll wait before verifying the activity.
If is not defined, wait half the `wait_time`.
"""
if monitor_time is None:
# if monitor time is not defined, then verify half
# the wait time since the last request
monitor_time = wait_time / 2
super().__init__(
bus, self._verify, monitor_time, self.__class__.__name__
)
# use a lock to make sure the thread that triggers the before_request
# and after_request does not collide with the monitor method (_verify)
self._active_request_lock = threading.Lock()
self._active_requests = 0
self._wait_time = wait_time
self._last_request_ts = time.time()
def _verify(self):
# verify that we don't have any active requests and
# shutdown the server in case we haven't seen any activity
# since self._last_request_ts + self._wait_time
with self._active_request_lock:
if (not self._active_requests and
self._last_request_ts + self._wait_time < time.time()):
self.bus.exit() # shutdown the engine
def before_request(self):
with self._active_request_lock:
self._active_requests += 1
def after_request(self):
with self._active_request_lock:
self._active_requests -= 1
# update the last time a request was served
self._last_request_ts = time.time()
class Root:
@cherrypy.expose
def index(self):
return "Hello user: current time {:.0f}".format(time.time())
def main():
# here is how to use the plugin:
ActivityMonitor(cherrypy.engine, wait_time=10, monitor_time=5).subscribe()
cherrypy.quickstart(Root())
if __name__ == '__main__':
main()