torando-ioloop生命周期
Posted wenlin-gk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了torando-ioloop生命周期相关的知识,希望对你有一定的参考价值。
https://stackoverflow.com/questions/5375220/how-do-i-stop-tornado-web-server?answertab=votes#tab-top
http://y.tsutsumi.io/keyerror-in-self_handlers-a-journey-deep-into-tornados-internals.html
# coding=utf-8 """ServerRunner""" from threading import Thread from time import sleep from tornado.ioloop import IOLoop class ServerRunner(Thread): """优化:自动设置为deamon线程""" def __init__(self, *servers): super(ServerRunner, self).__init__(name="HTTPServer") self.__servers = servers self.__IOLoop = None def run(self): """override""" self.__start_servers() self.__IOLoop = IOLoop.current() self.__IOLoop.start() def stop(self): self.__stop_IO_loop() self.__stop_servers() def __start_servers(self): for server in self.__servers: server.start() def __stop_IO_loop(self): # 当ServerRunner#start()已调用,self.__IOLoop赋值语句还未执行时 if self.is_alive() and self.__IOLoop is None: sleep(0.5) # 等待线程启动 if self.__IOLoop is not None: self.__IOLoop.add_callback(self.__IOLoop.stop) self.__IOLoop = None def __stop_servers(self): for server in self.__servers: server.stop()
以上是关于torando-ioloop生命周期的主要内容,如果未能解决你的问题,请参考以下文章