
import os, sys

from .base import Gor

from tornado import gen, ioloop, queues


class TornadoGor(Gor):

    def __init__(self, *args, **kwargs):
        super(TornadoGor, self).__init__(*args, **kwargs)
        self.q = queues.Queue()
        self.concurrency = kwargs.get('concurrency', 2)

    @gen.coroutine
    def _process(self):
        line = yield self.q.get()
        try:
            msg = self.parse_message(line)
            if msg:
                self.emit(msg, line)
        finally:
            self.q.task_done()

    @gen.coroutine
    def _worker(self):
        while True:
            yield self._process()

    @gen.coroutine
    def _run(self):
        for _ in range(self.concurrency):
            self._worker()

        while True:
            try:
                line = sys.stdin.readline()
            except KeyboardInterrupt:
                try:
                    sys.exit(0)
                except SystemExit:
                    os._exit(0)
            self.q.put(line)
            yield

    def run(self):
        self.io_loop = ioloop.IOLoop.current()
        self.io_loop.run_sync(self._run)


import sys
import errno
import logging

from .base import Gor

from tornado import gen, ioloop, queues


import contextlib
from tornado.stack_context import StackContext

@contextlib.contextmanager
def die_on_error():
    try:
        yield
    except Exception:
        logging.error("exception in asynchronous operation", exc_info=True)
        sys.exit(1)


class TornadoGor(Gor):

    def __init__(self, *args, **kwargs):
        super(TornadoGor, self).__init__(*args, **kwargs)
        self.q = queues.Queue()
        self.concurrency = kwargs.get('concurrency', 2)

    @gen.coroutine
    def _process(self):
        line = yield self.q.get()
        try:
            msg = self.parse_message(line)
            if msg:
                self.emit(msg, line)
        finally:
            self.q.task_done()

    @gen.coroutine
    def _worker(self):
        while True:
            yield self._process()

    @gen.coroutine
    def _run(self):
        for _ in range(self.concurrency):
            self._worker()

        while True:
            try:
                line = sys.stdin.readline()
            except KeyboardInterrupt:
                ioloop.IOLoop.instance().stop()
                break
            self.q.put(line)
            yield

    def run(self):
        with StackContext(die_on_error):
            self.io_loop = ioloop.IOLoop.current()
            self.io_loop.run_sync(self._run)
            sys.exit(errno.EINTR)

Exit as soon as KeyboardInterrupt catched
Exit as soon as KeyboardInterrupt catched

  
- import os, sys
+ import sys
+ import errno
+ import logging
  
  from .base import Gor
  
  from tornado import gen, ioloop, queues
+ 
+ 
+ import contextlib
+ from tornado.stack_context import StackContext
+ 
+ @contextlib.contextmanager
+ def die_on_error():
+     try:
+         yield
+     except Exception:
+         logging.error("exception in asynchronous operation", exc_info=True)
+         sys.exit(1)
  
  
  class TornadoGor(Gor):
  
      def __init__(self, *args, **kwargs):
          super(TornadoGor, self).__init__(*args, **kwargs)
          self.q = queues.Queue()
          self.concurrency = kwargs.get('concurrency', 2)
  
      @gen.coroutine
      def _process(self):
          line = yield self.q.get()
          try:
              msg = self.parse_message(line)
              if msg:
                  self.emit(msg, line)
          finally:
              self.q.task_done()
  
      @gen.coroutine
      def _worker(self):
          while True:
              yield self._process()
  
      @gen.coroutine
      def _run(self):
          for _ in range(self.concurrency):
              self._worker()
  
          while True:
              try:
                  line = sys.stdin.readline()
              except KeyboardInterrupt:
+                 ioloop.IOLoop.instance().stop()
-                 try:
+                 break
-                     sys.exit(0)
-                 except SystemExit:
-                     os._exit(0)
              self.q.put(line)
              yield
  
      def run(self):
+         with StackContext(die_on_error):
-         self.io_loop = ioloop.IOLoop.current()
+             self.io_loop = ioloop.IOLoop.current()
-         self.io_loop.run_sync(self._run)
+             self.io_loop.run_sync(self._run)
+             sys.exit(errno.EINTR)
  
Exit as soon as KeyboardInterrupt catched
## Code Before:

import os, sys

from .base import Gor

from tornado import gen, ioloop, queues


class TornadoGor(Gor):

    def __init__(self, *args, **kwargs):
        super(TornadoGor, self).__init__(*args, **kwargs)
        self.q = queues.Queue()
        self.concurrency = kwargs.get('concurrency', 2)

    @gen.coroutine
    def _process(self):
        line = yield self.q.get()
        try:
            msg = self.parse_message(line)
            if msg:
                self.emit(msg, line)
        finally:
            self.q.task_done()

    @gen.coroutine
    def _worker(self):
        while True:
            yield self._process()

    @gen.coroutine
    def _run(self):
        for _ in range(self.concurrency):
            self._worker()

        while True:
            try:
                line = sys.stdin.readline()
            except KeyboardInterrupt:
                try:
                    sys.exit(0)
                except SystemExit:
                    os._exit(0)
            self.q.put(line)
            yield

    def run(self):
        self.io_loop = ioloop.IOLoop.current()
        self.io_loop.run_sync(self._run)

## Instruction:
Exit as soon as KeyboardInterrupt catched
## Code After:

import sys
import errno
import logging

from .base import Gor

from tornado import gen, ioloop, queues


import contextlib
from tornado.stack_context import StackContext

@contextlib.contextmanager
def die_on_error():
    try:
        yield
    except Exception:
        logging.error("exception in asynchronous operation", exc_info=True)
        sys.exit(1)


class TornadoGor(Gor):

    def __init__(self, *args, **kwargs):
        super(TornadoGor, self).__init__(*args, **kwargs)
        self.q = queues.Queue()
        self.concurrency = kwargs.get('concurrency', 2)

    @gen.coroutine
    def _process(self):
        line = yield self.q.get()
        try:
            msg = self.parse_message(line)
            if msg:
                self.emit(msg, line)
        finally:
            self.q.task_done()

    @gen.coroutine
    def _worker(self):
        while True:
            yield self._process()

    @gen.coroutine
    def _run(self):
        for _ in range(self.concurrency):
            self._worker()

        while True:
            try:
                line = sys.stdin.readline()
            except KeyboardInterrupt:
                ioloop.IOLoop.instance().stop()
                break
            self.q.put(line)
            yield

    def run(self):
        with StackContext(die_on_error):
            self.io_loop = ioloop.IOLoop.current()
            self.io_loop.run_sync(self._run)
            sys.exit(errno.EINTR)
