def sendfile(self, fpath, size, callback=None): self._check_closed() if not "ws_buffer" in dir(self): self.ws_buffer = deque() if not "on_queue_change" in dir(self): def on_queue_change(change): pass self.on_queue_change = on_queue_change if self._write_buffer: append_wbuf(self, self._write_buffer) self._write_buffer = None # не используем def replace_meth(name, sf_meth): #old = getattr(self, name) def wrapper(*args, **kw): #if self.is_sendfile_on: #res = sf_meth(self, *args, **kw) #else: #res = old(*args, **kw) #return res return sf_meth(self, *args, **kw) setattr(self, name, wrapper) replace_meth("_handle_write", handle_write) replace_meth("writing", writing) replace_meth("write", write_to_stream) orig_close_fd = self.close_fd def close_fd(self): handle_close_fd(self) orig_close_fd() replace_meth("close_fd", close_fd) is_ok = True open_by_request = True if open_by_request: f = None else: # :TRICKY: жестокая реальность говорит нам, что файлы # могут не открываться, "Too many open files" => тогда # ничего не остается, кроме как закрыть поток, иначе будем # писать ерунду is_ok, f = try_open(fpath) if is_ok: # :TRICKY: нельзя менять значения tuple'а #SFClass = collections.namedtuple('SFClass', ['f', 'off', 'sz']) #sf = SFClass(open(fpath, "rb"), 0, size) sf = api.make_struct( f=f, off=0, sz=size, fpath=fpath, ) append_to_q(self, True, sf) try_write(self, callback) else: self.close() if callback: callback()
if callback: callback(None) io_loop.stop() process() io_loop.start() if True: test_timer = False if test_timer: def on_test_timer(): print("!") set_timer(1, on_test_timer) else: def write_func(stream, is_first): stream.write(b"Hello\n") print("written!") obj = api.make_struct() def on_timeout(): connect_to_dvr(obj, (host, port), write_func) set_timer(1, on_timeout) io_loop.start()
stream = yield gen.Task(connect, host, port) if stream: stream.close() if callback: callback(None) io_loop.stop() process() io_loop.start() if True: test_timer = False if test_timer: def on_test_timer(): print("!") set_timer(1, on_test_timer) else: def write_func(stream, is_first): stream.write(b"Hello\n") print("written!") obj = api.make_struct() def on_timeout(): connect_to_dvr(obj, (host, port), write_func) set_timer(1, on_timeout) io_loop.start()
def sendfile(self, fpath, size, callback=None): self._check_closed() if not "ws_buffer" in dir(self): self.ws_buffer = deque() if not "on_queue_change" in dir(self): def on_queue_change(change): pass self.on_queue_change = on_queue_change if self._write_buffer: append_wbuf(self, self._write_buffer) self._write_buffer = None # не используем def replace_meth(name, sf_meth): #old = getattr(self, name) def wrapper(*args, **kw): #if self.is_sendfile_on: #res = sf_meth(self, *args, **kw) #else: #res = old(*args, **kw) #return res return sf_meth(self, *args, **kw) setattr(self, name, wrapper) replace_meth("_handle_write", handle_write) replace_meth("writing", writing) replace_meth("write", write_to_stream) orig_close_fd = self.close_fd def close_fd(self): handle_close_fd(self) orig_close_fd() replace_meth("close_fd", close_fd) is_ok = True open_by_request = True if open_by_request: f = None else: # :TRICKY: жестокая реальность говорит нам, что файлы # могут не открываться, "Too many open files" => тогда # ничего не остается, кроме как закрыть поток, иначе будем # писать ерунду is_ok, f = try_open(fpath) if is_ok: # :TRICKY: нельзя менять значения tuple'а #SFClass = collections.namedtuple('SFClass', ['f', 'off', 'sz']) #sf = SFClass(open(fpath, "rb"), 0, size) sf = api.make_struct( f = f, off = 0, sz = size, fpath = fpath, ) append_to_q(self, True, sf) try_write(self, callback) else: self.close() if callback: callback()