本文整理汇总了Python中selectors.EVENT_WRITE属性的典型用法代码示例。如果您正苦于以下问题:Python selectors.EVENT_WRITE属性的具体用法?Python selectors.EVENT_WRITE怎么用?Python selectors.EVENT_WRITE使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类selectors
的用法示例。
在下文中一共展示了selectors.EVENT_WRITE属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: register
# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import EVENT_WRITE [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: _process_event
# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import EVENT_WRITE [as 别名]
def _process_event(self, key, mask):
"""Selector has delivered us an event."""
self._logger.debug('Processing event with key {} and mask {}'.format(key, mask))
fileobj, (reader, writer) = key.fileobj, key.data
if mask & selectors.EVENT_READ and reader is not None:
if reader._cancelled:
self.remove_reader(fileobj)
else:
self._logger.debug('Invoking reader callback: {}'.format(reader))
reader._run()
if mask & selectors.EVENT_WRITE and writer is not None:
if writer._cancelled:
self.remove_writer(fileobj)
else:
self._logger.debug('Invoking writer callback: {}'.format(writer))
writer._run()
示例3: test_unregister_after_fd_close_and_reuse
# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import EVENT_WRITE [as 别名]
def test_unregister_after_fd_close_and_reuse(self):
s = self.SELECTOR()
self.addCleanup(s.close)
rd, wr = self.make_socketpair()
r, w = rd.fileno(), wr.fileno()
s.register(r, selectors.EVENT_READ)
s.register(w, selectors.EVENT_WRITE)
rd2, wr2 = self.make_socketpair()
rd.close()
wr.close()
os.dup2(rd2.fileno(), r)
os.dup2(wr2.fileno(), w)
self.addCleanup(os.close, r)
self.addCleanup(os.close, w)
s.unregister(r)
s.unregister(w)
示例4: test_timeout
# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import EVENT_WRITE [as 别名]
def test_timeout(self):
s = self.SELECTOR()
self.addCleanup(s.close)
rd, wr = self.make_socketpair()
s.register(wr, selectors.EVENT_WRITE)
t = time()
self.assertEqual(1, len(s.select(0)))
self.assertEqual(1, len(s.select(-1)))
self.assertLess(time() - t, 0.5)
s.unregister(wr)
s.register(rd, selectors.EVENT_READ)
t = time()
self.assertFalse(s.select(0))
self.assertFalse(s.select(-1))
self.assertLess(time() - t, 0.5)
t0 = time()
self.assertFalse(s.select(1))
t1 = time()
dt = t1 - t0
# Tolerate 2.0 seconds for very slow buildbots
self.assertTrue(0.8 <= dt <= 2.0, dt)
示例5: __repr__
# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import EVENT_WRITE [as 别名]
def __repr__(self):
info = [self.__class__.__name__]
if self._pipe is None:
info.append('closed')
elif self._closing:
info.append('closing')
info.append(f'fd={self._fileno}')
selector = getattr(self._loop, '_selector', None)
if self._pipe is not None and selector is not None:
polling = selector_events._test_selector_event(
selector, self._fileno, selectors.EVENT_WRITE)
if polling:
info.append('polling')
else:
info.append('idle')
bufsize = self.get_write_buffer_size()
info.append(f'bufsize={bufsize}')
elif self._pipe is not None:
info.append('open')
else:
info.append('closed')
return '<{}>'.format(' '.join(info))
示例6: _remove_writer
# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import EVENT_WRITE [as 别名]
def _remove_writer(self, fd):
"""Remove a writer callback."""
if self.is_closed():
return False
try:
key = self._selector.get_key(fd)
except KeyError:
return False
else:
mask, (reader, writer) = key.events, key.data
# Remove both writer and connector.
mask &= ~selectors.EVENT_WRITE
if not mask:
self._selector.unregister(fd)
else:
self._selector.modify(fd, mask, (reader, None))
if writer is not None:
writer.cancel()
return True
else:
return False
示例7: service_connection
# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import EVENT_WRITE [as 别名]
def service_connection(key, mask):
sock = key.fileobj
data = key.data
if mask & selectors.EVENT_READ:
recv_data = sock.recv(BUFFER_SIZE)
if recv_data:
data.outb += recv_data
else:
print('Closing connection in {}'.format(data.addr))
selector.unregister(sock)
sock.close()
if mask & selectors.EVENT_WRITE:
if data.outb:
print('Echo from {} to {}'.format(repr(data.outb), data.addr))
sent = sock.send(data.outb)
data.outb = data.outb[sent:]
开发者ID:PacktPublishing,项目名称:Learning-Python-Networking-Second-Edition,代码行数:18,代码来源:tcp_server_selectors.py
示例8: start_connections
# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import EVENT_WRITE [as 别名]
def start_connections(host, port, num_conns):
server_address = (host, port)
for i in range(0, num_conns):
connid = i + 1
print('Starting connection {} towards {}'.format(connid, server_address))
socket_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# We connect using connect_ex () instead of connect ()
socket_tcp.connect_ex(server_address)
events = selectors.EVENT_READ | selectors.EVENT_WRITE
data = types.SimpleNamespace(connid=connid,
msg_total=sum(len(m) for m in messages),
recv_total=0,
messages=list(messages),
outb=b'')
selector.register(socket_tcp, events, data=data)
events = selector.select()
for key, mask in events:
service_connection(key, mask)
开发者ID:PacktPublishing,项目名称:Learning-Python-Networking-Second-Edition,代码行数:20,代码来源:tcp_client_selectors.py
示例9: service_connection
# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import EVENT_WRITE [as 别名]
def service_connection(key, mask):
sock = key.fileobj
data = key.data
if mask & selectors.EVENT_READ:
recv_data = sock.recv(BUFFER_SIZE)
if recv_data:
print('Received {} from connection {}'.format(repr(recv_data), data.connid))
data.recv_total += len(recv_data)
if not recv_data or data.recv_total == data.msg_total:
print('Closing connection', data.connid)
selector.unregister(sock)
sock.close()
if mask & selectors.EVENT_WRITE:
if not data.outb and data.messages:
data.outb = data.messages.pop(0).encode()
if data.outb:
print('Sending {} to connection {}'.format(repr(data.outb), data.connid))
sent = sock.send(data.outb)
sock.shutdown(socket.SHUT_WR)
data.outb = data.outb[sent:]
开发者ID:PacktPublishing,项目名称:Learning-Python-Networking-Second-Edition,代码行数:22,代码来源:tcp_client_selectors.py
示例10: test_select
# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import EVENT_WRITE [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)
示例11: _wr_shutdown
# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import EVENT_WRITE [as 别名]
def _wr_shutdown(self, conn, once=False):
"""action when connection should be write-shutdown
:type conn: socket.socket
"""
try:
conn.shutdown(socket.SHUT_WR)
except:
pass
if conn in self.conn_wr:
self.conn_wr.remove(conn)
if self.sel:
self._sel_disable_event(conn, EVENT_WRITE)
if not once and conn in self.map: # use the `once` param to avoid infinite loop
# pair should be rd_shutdown.
# if a socket is wr_shutdowned, then it's
self._rd_shutdown(self.map[conn], True)
示例12: test_add_reader_existing
# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import EVENT_WRITE [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)
示例13: test_add_writer_existing
# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import EVENT_WRITE [as 别名]
def test_add_writer_existing(self):
reader = mock.Mock()
writer = mock.Mock()
self.loop._selector.get_key.return_value = selectors.SelectorKey(
1, 1, selectors.EVENT_READ, (reader, writer))
cb = lambda: True
self.loop.add_writer(1, cb)
self.assertTrue(writer.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(reader, r)
self.assertEqual(cb, w._callback)
示例14: __on_write_activated
# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import EVENT_WRITE [as 别名]
def __on_write_activated(self, fd):
self._logger.debug('File {} ready to write'.format(fd))
key = self._key_from_fd(fd)
if key:
self.__parent._process_event(key, EVENT_WRITE & key.events)
示例15: test_unregister_after_fd_close
# 需要导入模块: import selectors [as 别名]
# 或者: from selectors import EVENT_WRITE [as 别名]
def test_unregister_after_fd_close(self):
s = self.SELECTOR()
self.addCleanup(s.close)
rd, wr = self.make_socketpair()
r, w = rd.fileno(), wr.fileno()
s.register(r, selectors.EVENT_READ)
s.register(w, selectors.EVENT_WRITE)
rd.close()
wr.close()
s.unregister(r)
s.unregister(w)