当前位置: 首页>>代码示例>>Python>>正文


Python selectors.SelectorKey方法代码示例

本文整理汇总了Python中selectors.SelectorKey方法的典型用法代码示例。如果您正苦于以下问题:Python selectors.SelectorKey方法的具体用法?Python selectors.SelectorKey怎么用?Python selectors.SelectorKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在selectors的用法示例。


在下文中一共展示了selectors.SelectorKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: register

# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import SelectorKey [as 别名]
def register(self, fileobj, events, data=None):
        if (not events) or (events & ~(EVENT_READ | EVENT_WRITE)):
            raise ValueError("Invalid events: {!r}".format(events))

        key = selectors.SelectorKey(fileobj, self._fileobj_lookup(fileobj), events, data)

        if key.fd in self._fd_to_key:
            raise KeyError("{!r} (FD {}) is already registered".format(fileobj, key.fd))

        self._fd_to_key[key.fd] = key

        if events & EVENT_READ:
            notifier = QtCore.QSocketNotifier(key.fd, QtCore.QSocketNotifier.Read)
            notifier.activated.connect(self.__on_read_activated)
            self.__read_notifiers[key.fd] = notifier
        if events & EVENT_WRITE:
            notifier = QtCore.QSocketNotifier(key.fd, QtCore.QSocketNotifier.Write)
            notifier.activated.connect(self.__on_write_activated)
            self.__write_notifiers[key.fd] = notifier

        return key 
开发者ID:gmarull,项目名称:asyncqt,代码行数:23,代码来源:_unix.py

示例2: test_register

# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import SelectorKey [as 别名]
def test_register(self):
        s = self.SELECTOR()
        self.addCleanup(s.close)

        rd, wr = self.make_socketpair()

        key = s.register(rd, selectors.EVENT_READ, "data")
        self.assertIsInstance(key, selectors.SelectorKey)
        self.assertEqual(key.fileobj, rd)
        self.assertEqual(key.fd, rd.fileno())
        self.assertEqual(key.events, selectors.EVENT_READ)
        self.assertEqual(key.data, "data")

        # register an unknown event
        self.assertRaises(ValueError, s.register, 0, 999999)

        # register an invalid FD
        self.assertRaises(ValueError, s.register, -10, selectors.EVENT_READ)

        # register twice
        self.assertRaises(KeyError, s.register, rd, selectors.EVENT_READ)

        # register the same FD, but with a different object
        self.assertRaises(KeyError, s.register, rd.fileno(),
                          selectors.EVENT_READ) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:27,代码来源:test_selectors.py

示例3: test_select

# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import SelectorKey [as 别名]
def test_select(self):
        s = self.SELECTOR()
        self.addCleanup(s.close)

        rd, wr = self.make_socketpair()

        s.register(rd, selectors.EVENT_READ)
        wr_key = s.register(wr, selectors.EVENT_WRITE)

        result = s.select()
        for key, events in result:
            self.assertTrue(isinstance(key, selectors.SelectorKey))
            self.assertTrue(events)
            self.assertFalse(events & ~(selectors.EVENT_READ |
                                        selectors.EVENT_WRITE))

        self.assertEqual([(wr_key, selectors.EVENT_WRITE)], result) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:19,代码来源:test_selectors.py

示例4: test_register_fileno

# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import SelectorKey [as 别名]
def test_register_fileno(self, selector, selector_mock):
        with open(os.devnull, 'r') as devnull:
            if selector_mock:
                returned_key = selectors.SelectorKey(
                    devnull, devnull.fileno(), selectors.EVENT_READ, "data"
                )
                selector_mock.register.side_effect = _mock_register(selector_mock, returned_key)

            key = selector.register(devnull, selectors.EVENT_READ, "data")

            self.assertEqual(key, selector.get_map()[devnull])

            if selector_mock:
                selector_mock.register.assert_called_with(devnull,
                                                          selectors.EVENT_READ,
                                                          "data") 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:18,代码来源:test_selector.py

示例5: test_modify_fileno

# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import SelectorKey [as 别名]
def test_modify_fileno(self, selector, selector_mock):
        with open(os.devnull, 'r') as devnull:
            if selector_mock:
                returned_key = selectors.SelectorKey(
                    devnull, devnull.fileno(), selectors.EVENT_READ, "data2"
                )
                selector_mock.modify.side_effect = _mock_register(selector_mock, returned_key)

            original_key = selector.register(devnull, selectors.EVENT_READ, "data")
            # modify may update the original key, keep a copy
            original_key = copy.copy(original_key)

            key = selector.modify(devnull, selectors.EVENT_READ, "data2")

            self.assertNotEqual(original_key, key)
            self.assertEqual(key, selector.get_map()[devnull])

            if selector_mock:
                selector_mock.modify.assert_called_with(devnull, selectors.EVENT_READ, "data2") 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:21,代码来源:test_selector.py

示例6: test_modify_fd

# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import SelectorKey [as 别名]
def test_modify_fd(self, selector, selector_mock):
        fd = 1

        if selector_mock:
            returned_key = selectors.SelectorKey(fd, fd, selectors.EVENT_READ, "data2")
            selector_mock.modify.side_effect = _mock_register(selector_mock, returned_key)

        original_key = selector.register(fd, selectors.EVENT_READ, "data")
        original_key = copy.copy(original_key)

        key = selector.modify(fd, selectors.EVENT_READ, "data2")

        self.assertNotEqual(original_key, key)
        self.assertEqual(key, selector.get_map()[fd])

        if selector_mock:
            selector_mock.modify.assert_called_with(fd, selectors.EVENT_READ, "data2") 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:19,代码来源:test_selector.py

示例7: test_fail_on_original_selector_callback

# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import SelectorKey [as 别名]
def test_fail_on_original_selector_callback(self):
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)

        try:
            with unittest.mock.patch.object(loop, "_selector") as mock:
                class TestCase(asynctest.TestCase):
                    use_default_loop = True

                    def runTest(self):
                        # add a dummy event
                        handle = asyncio.Handle(lambda: None, (), self.loop)
                        key = selectors.SelectorKey(1, 1, selectors.EVENT_READ,
                                                    (handle, None))
                        mock.get_map.return_value = {1: key}

                with self.assertRaisesRegex(AssertionError,
                                            "some events watched during the "
                                            "tests were not removed"):
                    TestCase().debug()
        finally:
            loop.close()
            asyncio.set_event_loop(None) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:25,代码来源:test_selector.py

示例8: test_add_reader_existing

# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import SelectorKey [as 别名]
def test_add_reader_existing(self):
        reader = mock.Mock()
        writer = mock.Mock()
        self.loop._selector.get_key.return_value = selectors.SelectorKey(
            1, 1, selectors.EVENT_WRITE, (reader, writer))
        cb = lambda: True
        self.loop.add_reader(1, cb)

        self.assertTrue(reader.cancel.called)
        self.assertFalse(self.loop._selector.register.called)
        self.assertTrue(self.loop._selector.modify.called)
        fd, mask, (r, w) = self.loop._selector.modify.call_args[0]
        self.assertEqual(1, fd)
        self.assertEqual(selectors.EVENT_WRITE | selectors.EVENT_READ, mask)
        self.assertEqual(cb, r._callback)
        self.assertEqual(writer, w) 
开发者ID:bkerler,项目名称:android_universal,代码行数:18,代码来源:test_selector_events.py

示例9: register

# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import SelectorKey [as 别名]
def register(self, fileobj, events, data=None):
        key = selectors.SelectorKey(fileobj, 0, events, data)
        self.keys[fileobj] = key
        return key 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:6,代码来源:test_telnetlib.py

示例10: test_register_file

# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import SelectorKey [as 别名]
def test_register_file(self):
        # epoll(7) returns EPERM when given a file to watch
        s = self.SELECTOR()
        with tempfile.NamedTemporaryFile() as f:
            with self.assertRaises(IOError):
                s.register(f, selectors.EVENT_READ)
            # the SelectorKey has been removed
            with self.assertRaises(KeyError):
                s.get_key(f) 
开发者ID:IronLanguages,项目名称:ironpython3,代码行数:11,代码来源:test_selectors.py

示例11: test_register_bad_fd

# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import SelectorKey [as 别名]
def test_register_bad_fd(self):
        # a file descriptor that's been closed should raise an OSError
        # with EBADF
        s = self.SELECTOR()
        bad_f = support.make_bad_fd()
        with self.assertRaises(OSError) as cm:
            s.register(bad_f, selectors.EVENT_READ)
        self.assertEqual(cm.exception.errno, errno.EBADF)
        # the SelectorKey has been removed
        with self.assertRaises(KeyError):
            s.get_key(bad_f) 
开发者ID:IronLanguages,项目名称:ironpython3,代码行数:13,代码来源:test_selectors.py

示例12: test_unregister_fileno

# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import SelectorKey [as 别名]
def test_unregister_fileno(self, selector, selector_mock):
        with open(os.devnull, 'r') as devnull:
            if selector_mock:
                key = selectors.SelectorKey(devnull, devnull.fileno(),
                                            selectors.EVENT_READ, "data")
                selector_mock.register.side_effect = _mock_register(selector_mock, key)
                selector_mock.unregister.side_effect = _mock_unregister(selector_mock, key)

            selector.register(devnull, selectors.EVENT_READ, "data")

            selector.unregister(devnull)

            self.assertNotIn(devnull, selector.get_map())
            self.assertNotIn(devnull.fileno(), selector.get_map()) 
开发者ID:Martiusweb,项目名称:asynctest,代码行数:16,代码来源:test_selector.py

示例13: _sel_disable_event

# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import SelectorKey [as 别名]
def _sel_disable_event(self, conn, ev):
        try:
            _key = self.sel.get_key(conn)  # type:selectors.SelectorKey
        except KeyError:
            pass
        else:
            if _key.events == EVENT_READ_WRITE:
                self.sel.modify(conn, EVENT_READ_WRITE ^ ev)
            else:
                self.sel.unregister(conn) 
开发者ID:aploium,项目名称:shootback,代码行数:12,代码来源:common_func.py

示例14: test_add_reader_existing_writer

# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import SelectorKey [as 别名]
def test_add_reader_existing_writer(self):
        writer = mock.Mock()
        self.loop._selector.get_key.return_value = selectors.SelectorKey(
            1, 1, selectors.EVENT_WRITE, (None, writer))
        cb = lambda: True
        self.loop.add_reader(1, cb)

        self.assertFalse(self.loop._selector.register.called)
        self.assertTrue(self.loop._selector.modify.called)
        fd, mask, (r, w) = self.loop._selector.modify.call_args[0]
        self.assertEqual(1, fd)
        self.assertEqual(selectors.EVENT_WRITE | selectors.EVENT_READ, mask)
        self.assertEqual(cb, r._callback)
        self.assertEqual(writer, w) 
开发者ID:bkerler,项目名称:android_universal,代码行数:16,代码来源:test_selector_events.py

示例15: test_remove_reader

# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import SelectorKey [as 别名]
def test_remove_reader(self):
        self.loop._selector.get_key.return_value = selectors.SelectorKey(
            1, 1, selectors.EVENT_READ, (None, None))
        self.assertFalse(self.loop.remove_reader(1))

        self.assertTrue(self.loop._selector.unregister.called) 
开发者ID:bkerler,项目名称:android_universal,代码行数:8,代码来源:test_selector_events.py


注:本文中的selectors.SelectorKey方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。