Ejemplo n.º 1
0
    def initialize(self, impl, time_func=None, **kwargs):
        super(PollIOLoop, self).initialize(**kwargs)
        self._impl = impl
        if hasattr(self._impl, 'fileno'):
            set_close_exec(self._impl.fileno())
        self.time_func = time_func or time.time
        self._handlers = {}
        self._events = {}
        self._callbacks = collections.deque()
        self._timeouts = []
        self._cancellations = 0
        self._running = False
        self._stopped = False
        self._closing = False
        self._thread_ident = None
        self._pid = os.getpid()
        self._blocking_signal_threshold = None
        self._timeout_counter = itertools.count()

        # Create a pipe that we send bogus data to when we want to wake
        # the I/O loop when it is idle
        self._waker = Waker()
        self.add_handler(self._waker.fileno(),
                         lambda fd, events: self._waker.consume(),
                         self.READ)
Ejemplo n.º 2
0
    def initialize(self, impl, time_func=None, **kwargs):
        super(PollIOLoop, self).initialize(**kwargs)
        self._impl = impl
        if hasattr(self._impl, 'fileno'):
            set_close_exec(self._impl.fileno())
        self.time_func = time_func or time.time
        # key:fd value:(fd_obj, handler)
        self._handlers = {}
        # key:fd value:events
        self._events = {}
        self._callbacks = []
        self._callback_lock = threading.Lock()
        self._timeouts = []
        self._cancellations = 0
        self._running = False
        self._stopped = False
        self._closing = False
        self._thread_ident = None
        self._blocking_signal_threshold = None
        # 计时器id,方便计时器获取自己添加到ioloop的顺序,用于比较超时时间
        # 加入超时时间点相同,触发时候根据加入的顺序进行回调
        self._timeout_counter = itertools.count()

        # Create a pipe that we send bogus data to when we want to wake
        # the I/O loop when it is idle
        # 当添加fd或者停止ioloop时候用于从多路复用中唤醒
        self._waker = Waker()
        self.add_handler(self._waker.fileno(),
                         lambda fd, events: self._waker.consume(), self.READ)
Ejemplo n.º 3
0
    def initialize(self, impl, time_func=None):
        # 最上一层的基类 Configurable __new__ 中
        # instance = super(Configurable, cls).__new__(impl)
        # instance.initialize(**args)
        #  此时 ioloop.initialize(**args)
        # platform_ioloop.initialize(**args) 包括 impl 参数, 就是 PollIOLoop 派生类 class
        # PollIOLoop -> initialize(**args)
        # IOLoop -> initialize(**args)
        # self.make_current()
        super(PollIOLoop, self).initialize()
        self._impl = impl
        if hasattr(self._impl, 'fileno'):
            set_close_exec(self._impl.fileno())
        self.time_func = time_func or time.time
        self._handlers = {}
        self._events = {}
        self._callbacks = []
        self._callback_lock = threading.Lock()
        self._timeouts = []
        self._cancellations = 0
        self._running = False
        self._stopped = False
        self._closing = False
        self._thread_ident = None
        self._blocking_signal_threshold = None
        self._timeout_counter = itertools.count()

        # Create a pipe that we send bogus data to when we want to wake
        # the I/O loop when it is idle
        self._waker = Waker()
        self.add_handler(self._waker.fileno(),
                         lambda fd, events: self._waker.consume(), self.READ)
Ejemplo n.º 4
0
    def initialize(self, impl, time_func=None):
        super(PollIOLoop, self).initialize()
        self._impl = impl
        if hasattr(self._impl, 'fileno'):
            set_close_exec(self._impl.fileno())
        self.time_func = time_func or time.time
        self._handlers = {}
        self._events = {}
        self._callbacks = []
        self._callback_lock = threading.Lock()
        self._timeouts = []
        self._cancellations = 0
        self._running = False
        self._stopped = False
        self._closing = False
        self._thread_ident = None
        self._blocking_signal_threshold = None

        # Create a pipe that we send bogus data to when we want to wake
        # the I/O loop when it is idle
        # 在 PollIOLoop(IOLoop) 初始化的过程中创建了一个 Waker 对象,
        # 将 Waker 对象 fd 的【读】端注册到事件循环中并设定相应的回调函数
        # (这样做的好处是当事件循环阻塞而没有响应描述符出现,需要在最大 timeout 时间之前返回,
        # 就可以向这个管道发送一个字符)。

        # Waker 的使用:
        # 一种是在其他线程向 IOLoop 添加 callback 时使用,
        # 唤醒 IOLoop 同时会将控制权转移给 IOLoop 线程并完成特定请求。
        # 唤醒的方法向管道中写入一个字符'x'。
        # 另外,在 IOLoop的stop 函数中会调用self._waker.wake(),通过向管道写入'x'停止事件循环

        self._waker = Waker()
        self.add_handler(self._waker.fileno(),
                         lambda fd, events: self._waker.consume(), self.READ)
Ejemplo n.º 5
0
 def __init__(self):
     self._impl = epoll()
     self._waker = Waker()
     self.add_handler(self._waker.fileno(),
                      lambda fd, events: self._waker.consume(), self.READ)
     self._handlers = {}
     self._events = {}
     self.coroutine_futures = dict()
     self.waits = deque()
     self.callbacks = deque()
Ejemplo n.º 6
0
    def __init__(self, impl=None):
        self._impl = impl or _poll()
        if hasattr(self._impl, 'fileno'):
            set_close_exec(self._impl.fileno())
        self._handlers = {}
        self._events = {}
        self._callbacks = []
        self._timeouts = []
        self._running = False
        self._stopped = False
        self._thread_ident = None
        self._blocking_signal_threshold = None

        # Create a pipe that we send bogus data to when we want to wake
        # the I/O loop when it is idle
        self._waker = Waker()
        self.add_handler(self._waker.fileno(),
                         lambda fd, events: self._waker.consume(), self.READ)
Ejemplo n.º 7
0
    def initialize(self, impl, time_func=None):
        super(PollIOLoop, self).initialize()

        # 使用的模型,使用的哪一个 select epoll kqueue
        # ubuntu系统使用的epoll
        self._impl = impl

        # 不明白
        if hasattr(self._impl, 'fileno'):
            set_close_exec(self._impl.fileno())

        # 设置获取时间的函数
        self.time_func = time_func or time.time

        # self._handlers[fd] = (obj, stack_context.wrap(handler))
        # fd这个时候是一个数字,一般的时候是一个fd的对象
        self._handlers = {}

        # 保存每一次循环所得到的fd和事件对
        self._events = {}

        # 所有的callback函数的集合
        self._callbacks = []
        self._callback_lock = threading.Lock()

        # 所有需要延时执行的函数的集合
        self._timeouts = []

        self._cancellations = 0
        self._running = False  # IOLoop是不是已经运行了
        self._stopped = False  # IOLoop是不是已经停止了,为什么有两个
        self._closing = False
        self._thread_ident = None
        self._blocking_signal_threshold = None

        # Create a pipe that we send bogus data to when we want to wake
        # the I/O loop when it is idle
        # 这个真心不懂,以后研究
        self._waker = Waker()

        # 初始化的时候添加self._waker的一个读得socket到IOLoop里面
        self.add_handler(self._waker.fileno(),
                         lambda fd, events: self._waker.consume(), self.READ)
Ejemplo n.º 8
0
    def __init__(self, impl=None):
        self._impl = impl or _poll()           # Linux下即epoll
        if hasattr(self._impl, 'fileno'):      # 若支持,设置FD_CLOEXEC
            set_close_exec(self._impl.fileno())

        self._callback_lock = threading.Lock() # 使self._callbacks可用于多线程

        self._handlers = {}      # epoll中每个fd的处理函数Map
        self._events = {}        # epoll返回的待处理事件Map
        self._callbacks = []     # 用户加入的回调函数列表
        self._timeouts = []      # ioloop中基于时间的调度,是一个小根堆

        self._running = False      # 标记ioloop已经调用了start,还未调用stop
        self._stopped = False      # 标记ioloop循环已退出,或已调用了stop
        self._thread_ident = None  # 标记ioloop所运行的线程,以支持多线程访问
        self._blocking_signal_threshold = None

        self._waker = Waker()    # 创建一个管道,用于在其他线程调用add_callback时唤醒epoll_wait
        self.add_handler(self._waker.fileno(), lambda fd, events: self._waker.consume(), self.READ)
Ejemplo n.º 9
0
    def initialize(self, impl, time_func=None):
        super(PollIOLoop, self).initialize()
        self._impl = impl
        if hasattr(self._impl, 'fileno'):
            set_close_exec(self._impl.fileno())
        self.time_func = time_func or time.time
        self._handlers = {}
        self._events = {}
        self._callbacks = []
        self._callback_lock = threading.Lock()
        self._timeouts = []
        self._running = False
        self._stopped = False
        self._thread_ident = None
        self._blocking_signal_threshold = None

        # Create a pipe that we send bogus data to when we want to wake
        # the I/O loop when it is idle
        self._waker = Waker()
        self.add_handler(self._waker.fileno(),
                         lambda fd, events: self._waker.consume(), self.READ)
Ejemplo n.º 10
0
    def initialize(self, impl, time_func=None, **kwargs):
        super(PollIOLoop, self).initialize(**kwargs)
        self._impl = impl
        if hasattr(self._impl, 'fileno'):
            set_close_exec(self._impl.fileno())
        self.time_func = time_func or time.time
        self._handlers = {}
        self._events = {}
        self._callbacks = []
        self._callback_lock = threading.Lock()
        self._timeouts = []
        self._cancellations = 0
        self._running = False
        self._stopped = False
        self._closing = False
        self._thread_ident = None
        self._blocking_signal_threshold = None
        self._timeout_counter = itertools.count()

        self._waker = Waker()
        self.add_handler(self._waker.fileno(),
                         lambda fd, events: self._waker.consume(), self.READ)