Python signal.SIGINT Examples

The following are 30 code examples of signal.SIGINT(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module signal , or try the search function .
Example #1
Source File: test_break.py    From jawfish with MIT License 6 votes vote down vote up
def testSecondInterrupt(self):
        result = unittest.TestResult()
        unittest.installHandler()
        unittest.registerResult(result)

        def test(result):
            pid = os.getpid()
            os.kill(pid, signal.SIGINT)
            result.breakCaught = True
            self.assertTrue(result.shouldStop)
            os.kill(pid, signal.SIGINT)
            self.fail("Second KeyboardInterrupt not raised")

        try:
            test(result)
        except KeyboardInterrupt:
            pass
        else:
            self.fail("Second KeyboardInterrupt not raised")
        self.assertTrue(result.breakCaught) 
Example #2
Source File: mqtt2sql.py    From mqtt2sql with GNU General Public License v3.0 6 votes vote down vote up
def exitus(self, status=0, message="end"):
        """
        Called when the program should be exit

        @param status:
            the exit status program returns to callert
        @param message:
            the message logged before exit
        """
        # pylint: disable=global-statement
        global SCRIPTNAME
        global SCRIPTPID
        global VER
        # pylint: enable=global-statement
        if message is not None:
            log(1, message)
        log(0, '{}[{}] v{} end'.format(SCRIPTNAME, SCRIPTPID, VER))
        if status in (signal.SIGINT, signal.SIGTERM):
            status = 0
        sys.exit(status) 
Example #3
Source File: amqp_service_with_credentials.py    From tomodachi with MIT License 6 votes vote down vote up
def _started_service(self) -> None:
        async def publish(data: Any, routing_key: str) -> None:
            await amqp_publish(self, data, routing_key=routing_key, wait=True)

        async def _async() -> None:
            async def sleep_and_kill() -> None:
                await asyncio.sleep(10.0)
                if not self.closer.done():
                    self.closer.set_result(None)

            task = asyncio.ensure_future(sleep_and_kill())
            await self.closer
            if not task.done():
                task.cancel()
            os.kill(os.getpid(), signal.SIGINT)
        asyncio.ensure_future(_async())

        self.data_uuid = str(uuid.uuid4())
        await publish(self.data_uuid, 'test.topic') 
Example #4
Source File: test_non_packaged_imported_service.py    From tomodachi with MIT License 6 votes vote down vote up
def test_non_named_same_named_sub_service(monkeypatch: Any, capsys: Any, loop: Any) -> None:
    services, future = start_service('tests/services/test-copy/test-copy.py', monkeypatch)

    assert services is not None
    assert len(services) == 1
    instance = services.get('test_dummy')
    assert instance is not None
    assert instance.start is True
    assert instance.started is True
    assert instance.stop is False

    async def _async_kill():
        os.kill(os.getpid(), signal.SIGINT)

    loop.create_task(_async_kill())
    loop.run_until_complete(future)

    assert instance.stop is True 
Example #5
Source File: aws_sns_sqs_service_with_credentials_without_protocol.py    From tomodachi with MIT License 6 votes vote down vote up
def _started_service(self) -> None:
        async def publish(data: Any, topic: str) -> None:
            await aws_sns_sqs_publish(self, data, topic=topic, wait=False)

        async def _async() -> None:
            async def sleep_and_kill() -> None:
                await asyncio.sleep(10.0)
                if not self.closer.done():
                    self.closer.set_result(None)

            task = asyncio.ensure_future(sleep_and_kill())
            await self.closer
            if not task.done():
                task.cancel()
            os.kill(os.getpid(), signal.SIGINT)
        asyncio.ensure_future(_async())

        self.data_uuid = str(uuid.uuid4())
        for _ in range(30):
            if self.test_topic_data_received:
                break
            await publish(self.data_uuid, 'test-raw-topic')
            await asyncio.sleep(0.1) 
Example #6
Source File: test_protocol.py    From tomodachi with MIT License 6 votes vote down vote up
def test_protobuf_base_bad_proto_class(monkeypatch: Any, capsys: Any, loop: Any) -> None:
    services, future = start_service('tests/services/dummy_protobuf_service.py', monkeypatch)

    instance = services.get('test_dummy_protobuf')

    async def _async() -> None:
        data = Person()
        data.name = 'John Doe'
        data.id = '12'
        json_message = await instance.message_protocol.build_message(instance, 'topic', data)
        await instance.message_protocol.parse_message(json_message, str)

    with pytest.raises(AttributeError):
        loop.run_until_complete(_async())

    async def _async_kill():
        os.kill(os.getpid(), signal.SIGINT)

    loop.create_task(_async_kill())
    loop.run_until_complete(future) 
Example #7
Source File: test_protocol.py    From tomodachi with MIT License 6 votes vote down vote up
def test_protobuf_validation_no_proto_class(monkeypatch: Any, capsys: Any, loop: Any) -> None:
    services, future = start_service('tests/services/dummy_protobuf_service.py', monkeypatch)

    instance = services.get('test_dummy_protobuf')

    async def _async() -> None:
        instance.message_protocol.validate()

    with pytest.raises(Exception):
        loop.run_until_complete(_async())

    async def _async_kill():
        os.kill(os.getpid(), signal.SIGINT)

    loop.create_task(_async_kill())
    loop.run_until_complete(future) 
Example #8
Source File: test_protocol.py    From tomodachi with MIT License 6 votes vote down vote up
def test_protobuf_validation_bad_proto_class(monkeypatch: Any, capsys: Any, loop: Any) -> None:
    services, future = start_service('tests/services/dummy_protobuf_service.py', monkeypatch)

    instance = services.get('test_dummy_protobuf')

    async def _async() -> None:
        instance.message_protocol.validate(proto_class=str)

    with pytest.raises(Exception):
        loop.run_until_complete(_async())

    async def _async_kill():
        os.kill(os.getpid(), signal.SIGINT)

    loop.create_task(_async_kill())
    loop.run_until_complete(future) 
Example #9
Source File: test_protocol.py    From tomodachi with MIT License 6 votes vote down vote up
def test_protobuf_object_validation_function(monkeypatch: Any, capsys: Any, loop: Any) -> None:
    services, future = start_service(
        'tests/services/dummy_protobuf_service.py', monkeypatch)

    instance = services.get('test_dummy_protobuf')

    def test_validator(person: Person) -> None:
        validate_field_regex(person.name, r'^[a-zA-Z ]+$')

    async def _async() -> None:
        data = Person()
        data.name = 'John Doe'
        data.id = '12'
        protobuf_message = await instance.message_protocol.build_message(
            instance, 'topic', data)
        await instance.message_protocol.parse_message(
            protobuf_message, Person, test_validator)

    loop.run_until_complete(_async())

    async def _async_kill():
        os.kill(os.getpid(), signal.SIGINT)

    loop.create_task(_async_kill())
    loop.run_until_complete(future) 
Example #10
Source File: amqp_service_with_credentials_with_custom_protocol.py    From tomodachi with MIT License 6 votes vote down vote up
def _started_service(self) -> None:
        async def publish(data: Any, routing_key: str) -> None:
            await amqp_publish(self, data, routing_key=routing_key, wait=False, message_protocol=CustomProtocol)

        async def _async() -> None:
            async def sleep_and_kill() -> None:
                await asyncio.sleep(10.0)
                if not self.closer.done():
                    self.closer.set_result(None)

            task = asyncio.ensure_future(sleep_and_kill())
            await self.closer
            if not task.done():
                task.cancel()
            os.kill(os.getpid(), signal.SIGINT)
        asyncio.ensure_future(_async())

        self.data_uuid = str(uuid.uuid4())
        await publish(self.data_uuid, 'test.custom.topic') 
Example #11
Source File: test_non_packaged_imported_service.py    From tomodachi with MIT License 6 votes vote down vote up
def test_non_named_sub_service(monkeypatch: Any, capsys: Any, loop: Any) -> None:
    services, future = start_service('tests/services/test-copy/test.py', monkeypatch)

    assert services is not None
    assert len(services) == 1
    instance = services.get('test_dummy')
    assert instance is not None
    assert instance.start is True
    assert instance.started is True
    assert instance.stop is False

    async def _async_kill():
        os.kill(os.getpid(), signal.SIGINT)

    loop.create_task(_async_kill())
    loop.run_until_complete(future)

    assert instance.stop is True 
Example #12
Source File: test_break.py    From jawfish with MIT License 6 votes vote down vote up
def testRemoveResult(self):
        result = unittest.TestResult()
        unittest.registerResult(result)

        unittest.installHandler()
        self.assertTrue(unittest.removeResult(result))

        # Should this raise an error instead?
        self.assertFalse(unittest.removeResult(unittest.TestResult()))

        try:
            pid = os.getpid()
            os.kill(pid, signal.SIGINT)
        except KeyboardInterrupt:
            pass

        self.assertFalse(result.shouldStop) 
Example #13
Source File: test_break.py    From jawfish with MIT License 6 votes vote down vote up
def testHandlerReplacedButCalled(self):
        # If our handler has been replaced (is no longer installed) but is
        # called by the *new* handler, then it isn't safe to delay the
        # SIGINT and we should immediately delegate to the default handler
        unittest.installHandler()

        handler = signal.getsignal(signal.SIGINT)
        def new_handler(frame, signum):
            handler(frame, signum)
        signal.signal(signal.SIGINT, new_handler)

        try:
            pid = os.getpid()
            os.kill(pid, signal.SIGINT)
        except KeyboardInterrupt:
            pass
        else:
            self.fail("replaced but delegated handler doesn't raise interrupt") 
Example #14
Source File: test_break.py    From jawfish with MIT License 6 votes vote down vote up
def testInterruptCaught(self):
        default_handler = signal.getsignal(signal.SIGINT)

        result = unittest.TestResult()
        unittest.installHandler()
        unittest.registerResult(result)

        self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)

        def test(result):
            pid = os.getpid()
            os.kill(pid, signal.SIGINT)
            result.breakCaught = True
            self.assertTrue(result.shouldStop)

        try:
            test(result)
        except KeyboardInterrupt:
            self.fail("KeyboardInterrupt not handled")
        self.assertTrue(result.breakCaught) 
Example #15
Source File: test_dummy_service.py    From tomodachi with MIT License 6 votes vote down vote up
def test_dummy_service(monkeypatch: Any, capsys: Any, loop: Any) -> None:
    services, future = start_service('tests/services/dummy_service.py', monkeypatch)

    assert services is not None
    assert len(services) == 1
    instance = services.get('test_dummy')
    assert instance is not None
    assert instance.start is True
    assert instance.started is True
    assert instance.stop is False

    assert tomodachi.get_instance() == instance
    assert tomodachi.get_service() == instance
    assert tomodachi.get_service('test_dummy') == instance
    assert tomodachi.get_service('test_dummy_nonexistant') is None

    async def _async_kill():
        os.kill(os.getpid(), signal.SIGINT)

    loop.create_task(_async_kill())
    loop.run_until_complete(future)

    assert instance.stop is True 
Example #16
Source File: test_aws_sns_sqs_transport.py    From tomodachi with MIT License 6 votes vote down vote up
def test_publish_invalid_credentials(monkeypatch: Any, capsys: Any, loop: Any) -> None:
    services, future = start_service('tests/services/dummy_service.py', monkeypatch)

    instance = services.get('test_dummy')

    with pytest.raises(AWSSNSSQSException):
        loop.run_until_complete(AWSSNSSQSTransport.publish(instance, 'data', 'test-topic', wait=True))

    async def _async_kill():
        os.kill(os.getpid(), signal.SIGINT)

    loop.create_task(_async_kill())
    loop.run_until_complete(future)

    if not future.done():
        future.set_result(None)

    out, err = capsys.readouterr()
    assert 'The security token included in the request is invalid' in err
    assert out == '' 
Example #17
Source File: test_amqp_transport.py    From tomodachi with MIT License 6 votes vote down vote up
def test_publish_invalid_credentials(monkeypatch: Any, capsys: Any, loop: Any) -> None:
    services, future = start_service('tests/services/dummy_service.py', monkeypatch)

    instance = services.get('test_dummy')

    with pytest.raises(AmqpException):
        loop.run_until_complete(AmqpTransport.publish(instance, 'data', 'test.topic', wait=True))

    async def _async_kill():
        os.kill(os.getpid(), signal.SIGINT)

    loop.create_task(_async_kill())
    loop.run_until_complete(future)

    out, err = capsys.readouterr()
    assert 'Unable to connect [amqp] to 127.0.0.1:54321' in err
    assert out == '' 
Example #18
Source File: test_relative_imports.py    From tomodachi with MIT License 6 votes vote down vote up
def test_relative_import_service(monkeypatch: Any, capsys: Any, loop: Any) -> None:
    services, future = start_service('tests/services/relative_service.py', monkeypatch)

    assert services is not None
    assert len(services) == 1
    instance = services.get('test_relative')
    assert instance is not None
    assert instance.start is True
    assert instance.started is True
    assert instance.stop is False

    async def _async_kill():
        os.kill(os.getpid(), signal.SIGINT)

    loop.create_task(_async_kill())
    loop.run_until_complete(future)

    assert instance.stop is True 
Example #19
Source File: amqp_service_with_credentials_without_protocol.py    From tomodachi with MIT License 6 votes vote down vote up
def _started_service(self) -> None:
        async def publish(data: Any, routing_key: str) -> None:
            await amqp_publish(self, data, routing_key=routing_key, wait=False)

        async def _async() -> None:
            async def sleep_and_kill() -> None:
                await asyncio.sleep(10.0)
                if not self.closer.done():
                    self.closer.set_result(None)

            task = asyncio.ensure_future(sleep_and_kill())
            await self.closer
            if not task.done():
                task.cancel()
            os.kill(os.getpid(), signal.SIGINT)
        asyncio.ensure_future(_async())

        self.data_uuid = str(uuid.uuid4())
        await publish(self.data_uuid, 'test.raw.topic') 
Example #20
Source File: aws_sns_sqs_service_with_credentials.py    From tomodachi with MIT License 6 votes vote down vote up
def _started_service(self) -> None:
        async def publish(data: Any, topic: str) -> None:
            await aws_sns_sqs_publish(self, data, topic=topic, wait=False)

        async def _async() -> None:
            async def sleep_and_kill() -> None:
                await asyncio.sleep(10.0)
                if not self.closer.done():
                    self.closer.set_result(None)

            task = asyncio.ensure_future(sleep_and_kill())
            await self.closer
            if not task.done():
                task.cancel()
            os.kill(os.getpid(), signal.SIGINT)
        asyncio.ensure_future(_async())

        self.data_uuid = str(uuid.uuid4())
        await publish(self.data_uuid, 'test-topic-unique')
        for _ in range(30):
            if self.test_topic_data_received:
                break
            await publish(self.data_uuid, 'test-topic')
            await asyncio.sleep(0.1) 
Example #21
Source File: aws_sns_sqs_service_with_credentials_with_custom_protocol.py    From tomodachi with MIT License 6 votes vote down vote up
def _started_service(self) -> None:
        async def publish(data: Any, topic: str) -> None:
            await aws_sns_sqs_publish(self, data, topic=topic, wait=False, message_protocol=CustomProtocol)

        async def _async() -> None:
            async def sleep_and_kill() -> None:
                await asyncio.sleep(10.0)
                if not self.closer.done():
                    self.closer.set_result(None)

            task = asyncio.ensure_future(sleep_and_kill())
            await self.closer
            if not task.done():
                task.cancel()
            os.kill(os.getpid(), signal.SIGINT)
        asyncio.ensure_future(_async())

        self.data_uuid = str(uuid.uuid4())
        for _ in range(30):
            if self.test_topic_data_received:
                break
            await publish(self.data_uuid, 'test-custom-topic')
            await asyncio.sleep(0.1) 
Example #22
Source File: test_run.py    From molotov with Apache License 2.0 5 votes vote down vote up
def test_sizing_multiprocess_interrupted(self):

        counters = SharedCounters("OK", "FAILED")

        @scenario()
        async def sizer(session):
            if random.randint(0, 10) == 1:
                counters["FAILED"] += 1
                raise AssertionError()
            else:
                counters["OK"] += 1

        async def _stop():
            await asyncio.sleep(2.0)
            os.kill(os.getpid(), signal.SIGINT)

        asyncio.ensure_future(_stop())
        stdout, stderr, rc = self._test_molotov(
            "--sizing",
            "-p",
            "3",
            "--sizing-tolerance",
            "90",
            "--console-update",
            "0",
            "-s",
            "sizer",
            "molotov.tests.test_run",
        )
        self.assertTrue("Sizing was not finished" in stdout) 
Example #23
Source File: ui.py    From recruit with Apache License 2.0 5 votes vote down vote up
def finish(self):
        """
        Restore the original SIGINT handler after finishing.

        This should happen regardless of whether the progress display finishes
        normally, or gets interrupted.
        """
        super(InterruptibleMixin, self).finish()
        signal(SIGINT, self.original_handler) 
Example #24
Source File: ui.py    From recruit with Apache License 2.0 5 votes vote down vote up
def handle_sigint(self, signum, frame):
        """
        Call self.finish() before delegating to the original SIGINT handler.

        This handler should only be in place while the progress display is
        active.
        """
        self.finish()
        self.original_handler(signum, frame) 
Example #25
Source File: test_protocol.py    From tomodachi with MIT License 5 votes vote down vote up
def test_protobuf_base(monkeypatch: Any, capsys: Any, loop: Any) -> None:
    services, future = start_service('tests/services/dummy_protobuf_service.py', monkeypatch)

    instance = services.get('test_dummy_protobuf')

    async def _async() -> None:
        data = Person()
        data.name = 'John Doe'
        data.id = '12'
        t1 = time.time()
        protobuf_message = await instance.message_protocol.build_message(instance, 'topic', data)
        t2 = time.time()
        result, message_uuid, timestamp = await instance.message_protocol.parse_message(protobuf_message, Person)
        assert type(result.get('data')) is Person
        assert result.get('data') == data
        assert result.get('metadata', {}).get('data_encoding') == 'proto'
        assert result.get('data') == data
        assert result.get('data').name == data.name
        assert result.get('data').id == data.id
        assert len(MessageToJson(result.get('data'))) == len(MessageToJson(data))
        assert MessageToJson(result.get('data')) == MessageToJson(data)
        assert len(message_uuid) == 73
        assert message_uuid[0:36] == instance.uuid
        assert timestamp >= t1
        assert timestamp <= t2

    loop.run_until_complete(_async())

    async def _async_kill():
        os.kill(os.getpid(), signal.SIGINT)

    loop.create_task(_async_kill())
    loop.run_until_complete(future) 
Example #26
Source File: amqp_service_invalid_credentials.py    From tomodachi with MIT License 5 votes vote down vote up
def _started_service(self) -> None:
        async def _async() -> None:
            async def sleep_and_kill() -> None:
                await asyncio.sleep(10.0)
                if not self.closer.done():
                    self.closer.set_result(None)

            task = asyncio.ensure_future(sleep_and_kill())
            await self.closer
            if not task.done():
                task.cancel()
            os.kill(os.getpid(), signal.SIGINT)
        asyncio.ensure_future(_async()) 
Example #27
Source File: schedule_service.py    From tomodachi with MIT License 5 votes vote down vote up
def _started_service(self) -> None:
        async def _async() -> None:
            async def sleep_and_kill() -> None:
                await asyncio.sleep(8.0)
                if not self.closer.done():
                    self.closer.set_result(None)

            task = asyncio.ensure_future(sleep_and_kill())
            await self.closer
            if not task.done():
                task.cancel()
            os.kill(os.getpid(), signal.SIGINT)
        asyncio.ensure_future(_async()) 
Example #28
Source File: decorated_functions_service.py    From tomodachi with MIT License 5 votes vote down vote up
def _started_service(self) -> None:
        async def _async() -> None:
            async def sleep_and_kill() -> None:
                await asyncio.sleep(10.0)
                if not self.closer.done():
                    self.closer.set_result(None)

            task = asyncio.ensure_future(sleep_and_kill())
            await self.closer
            if not task.done():
                task.cancel()
            os.kill(os.getpid(), signal.SIGINT)
        asyncio.ensure_future(_async()) 
Example #29
Source File: start_process_service_schedule.py    From tomodachi with MIT License 5 votes vote down vote up
def _started_service(self) -> None:
        self.function_order.append('_started_service')

        async def _async() -> None:
            async def sleep_and_kill() -> None:
                await asyncio.sleep(12.0)
                if not self.closer.done():
                    self.closer.set_result(None)

            task = asyncio.ensure_future(sleep_and_kill())
            await self.closer
            if not task.done():
                task.cancel()
            os.kill(os.getpid(), signal.SIGINT)
        asyncio.ensure_future(_async()) 
Example #30
Source File: start_process_service_http_1.py    From tomodachi with MIT License 5 votes vote down vote up
def _started_service(self) -> None:
        self.function_order.append('_started_service')

        async def _async() -> None:
            async def sleep_and_kill() -> None:
                await asyncio.sleep(4.0)
                if not self.closer.done():
                    self.closer.set_result(None)

            task = asyncio.ensure_future(sleep_and_kill())
            await self.closer
            if not task.done():
                task.cancel()
            os.kill(os.getpid(), signal.SIGINT)
        asyncio.ensure_future(_async())