本文整理汇总了Python中selectors.DefaultSelector.modify方法的典型用法代码示例。如果您正苦于以下问题:Python DefaultSelector.modify方法的具体用法?Python DefaultSelector.modify怎么用?Python DefaultSelector.modify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类selectors.DefaultSelector
的用法示例。
在下文中一共展示了DefaultSelector.modify方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SimpleServer
# 需要导入模块: from selectors import DefaultSelector [as 别名]
# 或者: from selectors.DefaultSelector import modify [as 别名]
class SimpleServer(object):
""" This is a simple server receiving and sending utf-8 plain text lines
over TCP/IP. It should use epoll or other efficient mechanisms if
available to support a large number of clients without cpu hogging.
All data sent to connections will be utf-8 encoded. All received lines
from clients are expected to be utf-8 encoded.
Lines longer than 10KB will be quietly dropped.
"""
def __init__(self, port, callback):
logging.basicConfig(level=logging.INFO)
self.clients_kept_for_sending = []
self.selector = DefaultSelector()
self.callback = callback
self.server_socket = socket.socket(family=socket.AF_INET6,
type=socket.SOCK_STREAM)
self.server_socket.bind(("::0", port))
self.server_socket.listen(5)
self.selector.register(self.server_socket, EVENT_READ, data=None)
self.server_socket.setblocking(False)
def close(self):
self.server_socket.close()
def _remove_socket(self, socket):
try:
self.selector.unregister(socket)
except KeyError:
pass
def _handle_new_client(self, socket, addr):
client = SimpleClient(socket, self, addr)
self.selector.register(socket, EVENT_READ, data=client)
self.callback(client)
def _mark_client_for_sending(self, client, marked):
if marked:
self.selector.modify(client.socket, EVENT_READ | EVENT_WRITE,
data=client)
else:
self.selector.modify(client.socket, EVENT_READ, data=client)
def tick(self, timeout=0):
""" This will check for any server events and process them.
If timeout is greater than zero, it will wait at most that
amount of time in seconds until something happens.
(This is useful if you want to save some cpu time and you have
nothing else to do)
For debugging purposes, it returns the amount of events processed.
"""
# get rid of old connections we kept around for sending:
entry_removed = True
while entry_removed:
entry_removed = False
for entry in self.clients_kept_for_sending:
if entry[1] <= time.time() or len(entry[0].send_buffer) == 0:
try:
entry[0].close()
except:
pass
self.clients_kept_for_sending.remove(entry)
entry_removed = True
# wait for socket events:
events = self.selector.select(timeout=timeout)
events_processed = 0
for key, mask in events:
if mask & EVENT_READ != 0:
events_processed = events_processed + 1
# handle accept for server socket:
if key.data == None:
conn, addr = self.server_socket.accept()
self._handle_new_client(conn, addr)
continue
# handle client socket receive:
key.data._do_recv()
continue
if mask & EVENT_WRITE != 0:
events_processed = events_processed + 1
# handle client socket send:
key.data._do_outstanding_send()
continue
logging.debugging("unknown socket event happening, bug?")
return events_processed