def add_timeout(self, deadline, callback): """Calls the given callback at the time deadline from the I/O loop. Returns a handle that may be passed to remove_timeout to cancel. """ timeout = _Timeout(deadline, stack_context.wrap(callback)) bisect.insort(self._timeouts, timeout) return timeout
def add_callback(self, callback): """Calls the given callback on the next I/O loop iteration. It is safe to call this method from any thread at any time. Note that this is the *only* method in IOLoop that makes this guarantee; all other interaction with the IOLoop must be done from that IOLoop's thread. add_callback() may be used to transfer control from other threads to the IOLoop's thread. """ self._callbacks.append(stack_context.wrap(callback)) self._wake()
def write(self, data, callback=None): """Write the given data to this stream. If callback is given, we call it when all of the buffered write data has been successfully written to the stream. If there was previously buffered write data and an old write callback, that callback is simply overwritten with this new callback. """ self._check_closed() self._write_buffer.append(data) self._add_io_state(self.io_loop.WRITE) self._write_callback = stack_context.wrap(callback)
def read_msg(self, callback): """Call callback when we read a complete message.""" assert not self._read_callback, "Already reading" self._read_msg_flag = True self._read_callback = stack_context.wrap(callback) while True: if self._read_from_buffer(): return self._check_closed() if self._read_to_buffer() == 0: break self._add_io_state(self.io_loop.READ)
def read_until(self, delimiter, callback): """Call callback when we read the given delimiter.""" assert not self._read_callback, "Already reading" self._read_delimiter = delimiter self._read_callback = stack_context.wrap(callback) while True: # See if we've already got the data from a previous read if self._read_from_buffer(): return self._check_closed() if self._read_to_buffer() == 0: break self._add_io_state(self.io_loop.READ)
def read_bytes(self, num_bytes, callback): """Call callback when we read the given number of bytes.""" assert not self._read_callback, "Already reading" if num_bytes == 0: callback("") return self._read_bytes = num_bytes self._read_callback = stack_context.wrap(callback) while True: if self._read_from_buffer(): return self._check_closed() if self._read_to_buffer() == 0: break self._add_io_state(self.io_loop.READ)
def read(self, callback, keep_reading=False): """Call callback when we read anything.""" assert not self._read_callback, "Already reading" self._read_flag = True self._read_callback = stack_context.wrap(callback) self._keep_reading = keep_reading while True: if self._read_from_buffer(): return # self._check_closed() if not self.socket: return try: if self._read_to_buffer() == 0: break except socket.error: self.close() return self._add_io_state(self.io_loop.READ)
def connect(self, address, callback=None): """Connects the socket to a remote address without blocking. May only be called if the socket passed to the constructor was not previously connected. The address parameter is in the same format as for socket.connect, i.e. a (host, port) tuple. If callback is specified, it will be called when the connection is completed. Note that it is safe to call IOStream.write while the connection is pending, in which case the data will be written as soon as the connection is ready. Calling IOStream read methods before the socket is connected works on some platforms but is non-portable. """ self._connecting = True try: self.socket.connect(address) except socket.error as e: # In non-blocking mode connect() always raises an exception if e.args[0] not in (errno.EINPROGRESS, errno.EWOULDBLOCK): raise self._connect_callback = stack_context.wrap(callback) self._add_io_state(self.io_loop.WRITE)
def set_close_callback(self, callback): """Call the given callback when the stream is closed.""" self._close_callback = stack_context.wrap(callback)
def add_handler(self, fd, handler, events): """Registers the given handler to receive the given events for fd.""" self._handlers[fd] = stack_context.wrap(handler) self._impl.register(fd, events | self.ERROR)