码迷,mamicode.com
首页 > 其他好文 > 详细

torando-ioloop生命周期

时间:2018-08-04 15:50:06      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:for   coding   ons   super   调用   tornado   http   add   imp   

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生命周期

标签:for   coding   ons   super   调用   tornado   http   add   imp   

原文地址:https://www.cnblogs.com/wenlin-gk/p/9418694.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!