class Pipes: handler = None buffer = '' def __init__(self): self.pipe = Pipe(Loop.default_loop()) def run(self, handler, pid): self.handler = handler self.pipe.connect( f'\\\\?\\pipe\\basembeddedpipes{pid}', lambda pipe_handle, _: pipe_handle.start_read(self.handle)) self.pipe.loop.run() def handle(self, _handle, data, _error): check_process(data) self.buffer += data.decode('utf-8') chunk = self.buffer.split(BOUNDARY) if len(chunk[-1]) != 0: self.buffer = chunk.pop() else: self.buffer = '' for item in chunk: self.handler(item) def write(self, obj, need_delete=True): if need_delete: obj['v'].pop(NEED_STOP, None) packet = json.dumps(obj) + BOUNDARY self.pipe.write(str.encode(packet))
def add_acceptor(self, descriptor, name): assert name in self.slots, 'wrong name given' pipe = Pipe(self.loop) pipe.open(descriptor) kwargs = dict(backlog=self.slots[name].listener.backlog) acceptor = self.acceptors[name] = self.Acceptor(name, pipe, **kwargs) acceptor.start()
def channel(self): """Wrapper for socket. Needed to pass descriptor to child process. """ pipe = Pipe(self.loop) pipe.open(self.socket.fileno()) return pipe
def __init__(self, loop, stream): self.fd = None self.stream = stream try: fd = self.fd = stream.fileno() except AttributeError: self.channel = None else: channel = self.channel = Pipe(loop) setattr(channel, 'bypass', True) channel.open(fd)
class Stream(object): """Wrapper for plain file descriptor.""" def __init__(self, loop, fd): self.loop = loop self.channel = Pipe(loop) self.fd = fd self._emitter = EventEmitter(loop) def start(self): self._emitter.subscribe("WRITE", self._on_write) self.channel.open(self.fd) self.channel.start_read(self._on_read) def write(self, data): self._emitter.publish("WRITE", data) def subscribe(self, listener): self._emitter.subscribe('READ', listener) def unsubscribe(self, listener): self._emitter.unsubscribe('READ', listener) def stop(self, all_events=False): if self.channel.active: self.channel.close() if all_events: self._emitter.close() def _on_write(self, evtype, data): self.channel.write(data) def _on_read(self, handle, data, error): if not data: return msg = dict(event='READ', data=data) self._emitter.publish('READ', msg)
def __init__(self, loop, fd): self.loop = loop self.channel = Pipe(loop) self.fd = fd self._emitter = EventEmitter(loop)
def _pipe(self): pipe = Pipe(self.loop) pipe.open(self.descriptor) return pipe
def __init__(self): self.pipe = Pipe(Loop.default_loop())