def __call__(self): """ Dispatch received request. """ request = self.request cancelled = Cancelled(request.sn) latency = self.plugin.latency if latency: sleep(latency) if not self.plugin.url or cancelled(): self.discard() return producer = self._producer(self.plugin) progress = Progress(request, producer) context = Context(request.sn, progress, cancelled) Context.set(context) producer.open() try: self.producer = producer self.send_started(request) result = self.plugin.dispatch(request) self.send_reply(request, result) self.commit() finally: producer.close() Context.set()
def main(): basicConfig(level=DEBUG) thing = Thing() context = Context('0', Progress(), Cancelled()) Context.set(context) call = Call(thing.echo, 10000) print(call())
def test_set(self): context = Context('1', Mock(), Mock()) Context.set(context) # set self.assertEqual(Context._current.inst, context) # clear Context.set() self.assertEqual(Context._current.inst, None)
def test_set(self): context = Context("1", Mock(), Mock()) Context.set(context) # set self.assertEqual(Context._current.inst, context) # clear Context.set() self.assertEqual(Context._current.inst, None)
def __call__(self): """ Relay to RMI context progress reporter. """ context = Context.current() context.progress.__dict__.update(self.payload.__dict__) context.progress.report()
def echo(self, n): context = Context.current() for x in range(n): context.progress.details = 'hello {}'.format(x) context.progress.report() sleep(0.25) return n
def __call__(self, pipe): """ Perform RMI on the child-side of the forked call as follows: - Reset the RMI context. - Invoke the method - Send result: retval, progress, raised exception. All output is sent to the parent using the inter-process pipe. :param pipe: A message pipe. :type pipe: gofer.mp.Pipe """ pipe.writer = Writer(pipe.writer.fd) try: pipe.reader.close() monitor = ParentMonitor(pipe.writer) monitor.start() context = Context.current() context.cancelled = self._not_cancelled() context.progress = Progress(pipe.writer) result = self.method(*self.args, **self.kwargs) reply = protocol.Result(result) reply.send(pipe.writer) except PipeBroken: log.debug('Pipe broken.') except Exception as e: log.exception(str(e)) reply = protocol.Raised(e) reply.send(pipe.writer)
def test_init(self): sn = '1' progress = Mock() cancelled = Mock() context = Context(sn, progress, cancelled) self.assertEqual(context.sn, sn) self.assertEqual(context.progress, progress) self.assertEqual(context.cancelled, cancelled)
def report(self, details): """ Report progress. :param details: The details to report. :type details: dict """ if not self.progress_reported: # not enabled return context = Context.current() context.progress.details = details context.progress.report()
def run(self, *command): """ Run the specified command. :param command: A command and parameters. :type command: Iterable :return: (status, {stdout:<str>, stderr:<str>}) :rtype: tuple """ details = { STDOUT: '', STDERR: '', } result = { STDOUT: '', STDERR: '', } context = Context.current() p = Popen(command, stdout=PIPE, stderr=PIPE) try: while True: n_read = 0 if context.cancelled(): p.terminate() break for fp, key in ((p.stdout, STDOUT), (p.stderr, STDERR)): line = fp.readline() if line: n_read += len(line) details[key] = line result[key] += line self.report(details) if not n_read: # EOF break p.stdout.close() p.stderr.close() status = p.wait() return status, result except OSError as e: return -1, str(e)
def __call__(self, pipe): """ Perform RMI on the child-side of the forked call as follows: - Reset the RMI context. - Invoke the method - Send result: retval, progress, raised exception. All output is sent to the parent using the inter-process pipe. :param pipe: A message pipe. :type pipe: gofer.mp.Writer """ try: context = Context.current() context.cancelled = lambda: False context.progress = Progress(pipe) result = self.method(*self.args, **self.kwargs) reply = protocol.Result(result) reply.send(pipe) except Exception, e: log.exception(utf8(e)) reply = protocol.Raised(e) reply.send(pipe)
def __call__(self, pipe): """ Perform RMI on the child-side of the forked call as follows: - Reset the RMI context. - Invoke the method - Send result: retval, progress, raised exception. All output is sent to the parent using the inter-process pipe. :param pipe: A message pipe. :type pipe: multiprocessing.Connection """ try: context = Context.current() context.cancelled = lambda: False context.progress = Progress(pipe) result = self.method(*self.args, **self.kwargs) reply = protocol.Result(result) reply.send(pipe) except Exception, e: log.exception(utf8(e)) reply = protocol.Raised(e) reply.send(pipe)
def __call__(self): """ Invoke the RMI as follows: - Fork - Start the monitor. - Read and dispatch reply messages. :return: Whatever method returned. """ pipe = Pipe() target = Target(self.method, *self.args, **self.kwargs) child = Process(target, pipe) monitor = Monitor(Context.current(), child) try: child.start() monitor.start() pipe.writer.close() retval = self.read(pipe.reader) return retval finally: pipe.close() monitor.stop() child.wait()
def __call__(self): """ Invoke the RMI as follows: - Fork - Start the monitor. - Read and dispatch reply messages. :return: Whatever method returned. """ inbound, outbound = Pipe() target = Target(self.method, *self.args, **self.kwargs) child = Process(target=target, args=(outbound,)) monitor = Monitor(Context.current(), child, outbound) try: child.start() monitor.start() retval = self.read(inbound) return retval finally: inbound.close() outbound.close() monitor.stop() child.join()