本文整理汇总了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
示例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)
示例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)
示例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")
示例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")
示例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")
示例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)
示例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)
示例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
示例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)
示例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)
示例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())
示例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)
示例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)
示例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)