本文整理汇总了Python中selectors.DefaultSelector.select方法的典型用法代码示例。如果您正苦于以下问题:Python DefaultSelector.select方法的具体用法?Python DefaultSelector.select怎么用?Python DefaultSelector.select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类selectors.DefaultSelector
的用法示例。
在下文中一共展示了DefaultSelector.select方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: selector
# 需要导入模块: from selectors import DefaultSelector [as 别名]
# 或者: from selectors.DefaultSelector import select [as 别名]
def selector():
sel = DefaultSelector()
sel.register(proc.stdout, EVENT_READ, 0)
sel.register(proc.stderr, EVENT_READ, 1)
while True:
for key, mask in sel.select():
yield key.data, decode(key.fileobj.readline(linesize))
示例2: __init__
# 需要导入模块: from selectors import DefaultSelector [as 别名]
# 或者: from selectors.DefaultSelector import select [as 别名]
class Loop:
def __init__(self):
self.sel = DefaultSelector()
self.queue = deque()
def create_task(self, coro):
sock = coro.send(None)
self.sel.register(sock, EVENT_WRITE, coro)
self.queue.append(coro)
def run(self):
while self.queue:
events = self.sel.select()
for key, _ in events:
coro = key.data
event = key.events
file_obj = key.fileobj
self.sel.unregister(file_obj)
try:
if event == EVENT_WRITE:
self.sel.register(file_obj, EVENT_READ, coro)
elif event == EVENT_READ:
self.sel.register(file_obj, EVENT_WRITE, coro)
coro.send(None)
except StopIteration:
self.queue.popleft()
except:
pass
else: return
示例3: __init__
# 需要导入模块: from selectors import DefaultSelector [as 别名]
# 或者: from selectors.DefaultSelector import select [as 别名]
class Loop:
def __init__(self):
self.ready = deque()
self.selector = DefaultSelector()
self.futures = {}
def create_task(self, task):
self.ready.append(task)
def run_forever(self):
while True:
while not self.ready:
completed_futures = [future for future in self.futures if not future.running()]
for future in completed_futures:
self.ready.append(self.futures.pop(future))
# so select() is blocking. If set a negative time out it won't block.
events = self.selector.select(-1)
# add these socket events and unregister them from listened to:
for key, _ in events:
self.ready.append(key.data) # add the task to the ready queue
self.selector.unregister(key.fileobj)
while self.ready:
self.current_task = self.ready.popleft()
# try to run current_task...
try:
# run task to next yield point
reason, what = self.current_task.send(None)
# reason, what = self.current_task.send(None)
if reason == 'waiting_to_accept':
self.selector.register(what, EVENT_READ, self.current_task)
elif reason == 'waiting_to_read':
self.selector.register(what, EVENT_READ, self.current_task)
elif reason == 'waiting_to_write':
self.selector.register(what, EVENT_WRITE, self.current_task)
elif reason == 'waiting_for_future':
self.futures[what] = self.current_task
else:
raise RuntimeError(
'Something bad happened... er. reason={}'.format(reason))
except StopIteration:
pass
async def sock_recv(self, sock, maxbytes):
# wait to read from the socket
await read_wait(sock)
return sock.recv(maxbytes)
async def sock_accept(self, sock):
# wait to read/hear from the socket
await accept_wait(sock)
return sock.accept()
async def sock_sendall(self, sock, data):
while data:
# wait to be able to write to the socket
await write_wait(sock)
nsent = sock.send(data)
data = data[nsent:]
def run_in_executor(self, executor, func, *args):
return executor.submit(func, *args)
示例4: selector
# 需要导入模块: from selectors import DefaultSelector [as 别名]
# 或者: from selectors.DefaultSelector import select [as 别名]
def selector():
sel = DefaultSelector()
sel.register(proc.stdout, EVENT_READ, 0)
sel.register(proc.stderr, EVENT_READ, 1)
while True:
ready = sel.select(line_timeout)
if not ready and line_timeout:
raise ProcessLineTimedOut("popen line timeout expired", getattr(proc, "argv", None), getattr(proc, "machine", None))
for key, mask in ready:
yield key.data, decode(key.fileobj.readline(linesize))
示例5: IRC
# 需要导入模块: from selectors import DefaultSelector [as 别名]
# 或者: from selectors.DefaultSelector import select [as 别名]
class IRC(Service):
DEFAULT_PORT = 6667
TIMEOUT = 3
def __init__(self,events):
super().__init__(events,'irc')
self.servers = set()
self.selector = DefaultSelector()
self.events = events
self.has_servers = Event()
self.channels = {}#server:[channels]
def respond(self,addr,command):
if command['action']=='connect':
server = command['host']
nick = command['nick']
port = command.get('port',self.DEFAULT_PORT)
self.connect((server,port),nick)
if command['action']=='status':
print(self.servers)
self.broadcast({
'kind':'status',
'status':{
'servers':{
s: {
'channels':v
} for s,v in self.channels.items()
}
}
})
def connect(self,addr,nick):
host = addr[0]
if host not in self.servers:
print(nick)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print(addr)
sock.connect(addr)
self.events.listen(host,self.check_channels)
self.selector.register(sock,EVENT_READ|EVENT_WRITE,ChatServerInterface(addr[0],nick,self.events))
self.servers.add(host)
self.channels[host] = list()
if not self.has_servers.is_set():
self.has_servers.set()
def check_channels(self,addr,event):
if event['kind']=='irc':
if event['command']=='332':
print(event['event'],event['arguments'])
self.channels[event['event']].append(event['arguments'][1].lower())
def loop(self):
while True:
self.has_servers.wait()
for key,mask in self.selector.select():
if mask&EVENT_READ==EVENT_READ:
key.data.read_data(key.fileobj)
if mask&EVENT_WRITE==EVENT_WRITE:
key.data.write_data(key.fileobj)
示例6: loop
# 需要导入模块: from selectors import DefaultSelector [as 别名]
# 或者: from selectors.DefaultSelector import select [as 别名]
def loop():
pre_loop_barrier.wait(1)
selector = DefaultSelector()
selector.register(sig_r_fd, EVENT_READ)
with suppress(StopIteration):
while True:
events = selector.select(timeout=5)
if len(events) == 0:
raise StopIteration
for key, mask in events:
if key.fd == sig_r_fd:
data = os.read(sig_r_fd, 4)
self._sigchld_caught = True
raise StopIteration
self._post_loop_barrier.wait(1)
示例7: __init__
# 需要导入模块: from selectors import DefaultSelector [as 别名]
# 或者: from selectors.DefaultSelector import select [as 别名]
class Loop:
def __init__(self):
self.ready = deque()
self.selector = DefaultSelector()
async def sock_recv(self, sock, maxbytes):
await read_wait(sock)
return sock.recv(maxbytes)
async def sock_accept(self, sock):
await read_wait(sock)
return sock.accept()
async def sock_sendall(self, sock, data):
while data:
try:
nsent = sock.send(data)
data = data[nsent:]
except BlockingIOError:
await write_wait(sock)
def create_task(self, coro):
self.ready.append(coro)
def run_forever(self):
while True:
while not self.ready:
events = self.selector.select()
for key, _ in events:
self.ready.append(key.data)
self.selector.unregister(key.fileobj)
while self.ready:
self.current_task = self.ready.popleft()
try:
op, *args = self.current_task.send(None)
getattr(self, op)(*args)
except StopIteration:
pass
def read_wait(self, sock):
self.selector.register(sock, EVENT_READ, self.current_task)
def write_wait(self, sock):
self.selector.register(sock, EVENT_WRITE, self.current_task)
示例8: __init__
# 需要导入模块: from selectors import DefaultSelector [as 别名]
# 或者: from selectors.DefaultSelector import select [as 别名]
class Loop:
def __init__(self):
self.ready = deque()
self.selector = DefaultSelector()
def create_task(self, task):
self.ready.append(task)
def run_forever(self):
while True:
# hmmn, nothing to run -> must be waiting on stuff...
while not self.ready:
events = self.selector.select()
# add these events and unregister them from listened to:
for key, _ in events:
self.ready.append(key.data)
self.selector.unregister(key.fileobj)
while self.ready:
self.current_task = self.ready.popleft()
# try to run current_task...
try:
# run task to next yield point
reason, sock = next(self.current_task)
if reason == 'waiting_to_accept':
self.selector.register(sock, EVENT_READ, self.current_task)
elif reason == 'waiting_to_read':
self.selector.register(sock, EVENT_READ, self.current_task)
elif reason == 'waiting_to_write':
self.selector.register(sock, EVENT_WRITE, self.current_task)
else:
raise RuntimeError('Something bad happened... er. reason={}'.format(reason))
except StopIteration:
pass
def sock_recv(self, sock, maxbytes):
# wait to read from the socket
return sock.recv(maxbytes)
def sock_accept(self, sock):
# wait to read/hear from the socket
return sock.accept()
def sock_sendall(self, sock, data):
while data:
# wait to be able to write to the socket
nsent = sock.send(data)
data = data[nsent:]
示例9: __init__
# 需要导入模块: from selectors import DefaultSelector [as 别名]
# 或者: from selectors.DefaultSelector import select [as 别名]
class Loop:
def __init__(self):
self.ready = deque()
self.selectors = DefaultSelector() # a way to watch socket IO
async def sock_recv(self, sock, maxbytes):
await read_wait(sock) # wait for something happen
return sock.recv(maxbytes)
async def sock_accept(self, sock):
await read_wait(sock)
return sock.sock_accept()
async def sock_sendall(self, sock, data):
while data :
try:
nsent = sock.send(data)
data = data[nsent:] # send partial data
except BlockingIOError:
await write_wait(sock)
def create_task(self, coro):
self.ready.append(coro)
def run_forever(self):
while True:
while not self.ready:
events = self.selectors.select()
for key, _ in events:
self.ready.append(key.data)
self.selectors.unregister(key.fileobj)
while self.ready:
self.current_task = self.ready.popleft()
try:
op, *args = self.current_task.send(None) # run to the yield
getattr(self,op)(*args) # Sneaky method call
except StopIteration:
pass
def read_wait(self, sock):
self.selectors.register(sock, EVENT_READ, self.current_task)
def write_wait(self, sock):
self.selectors.register
示例10: __init__
# 需要导入模块: from selectors import DefaultSelector [as 别名]
# 或者: from selectors.DefaultSelector import select [as 别名]
class TCPServer:
def __init__(self, port):
self.port = port
self.sock = None
self.selector = DefaultSelector()
# Advance self.listen() method. A future has been attached to the
# server socket, that has been registered in selector. And a callback
# has been attached to this future.
Task(self.listen())
def run(self):
while True:
ready = self.selector.select()
for key, events in ready:
fut = key.data
fut.resolve()
def listen(self):
server_sock = self._create_socket()
with server_sock:
try:
client_sock, remote_addr = self.sock.accept()
except BlockingIOError:
pass
fut = Future()
self.selector.register(self.sock, EVENT_READ, fut)
while True:
yield fut
Task(self.handle_client())
def handle_client(self):
client_sock, remote_addr = self.sock.accept()
client_sock.setblocking(False)
client_name = '{} {}'.format(*client_sock.getpeername())
print('Connected', client_name)
# Register client socket for reading and wait for this event
fut = Future()
self.selector.register(client_sock, EVENT_READ, fut)
yield fut
buffer = []
while True:
try:
chunk = client_sock.recv(1024)
buffer.append(chunk)
except BlockingIOError:
# Yield the same future, so we don't need to register
# it again. By the way, callback is the same for all
# futures, so it doesn't matter in my case.
yield fut
continue
print('Received chunk from client', client_name)
if chunk.endswith(b'end'):
print('Got all data from client', client_name)
request = b''.join(buffer).decode('utf8')
response = (request.upper() * 100).encode('utf8')
self.selector.unregister(client_sock)
fut = Future()
self.selector.register(client_sock, EVENT_WRITE, fut)
break
# This future was attached to the client_sock
yield fut
while True:
try:
bytes_sent = client_sock.send(response)
print(bytes_sent, 'bytes sent to client', client_name)
response = response[bytes_sent:]
except BlockingIOError:
print('Socket blockes for', client_name)
yield fut
continue
else:
if not response:
# All response sent
print('Response sent to client', client_name)
self.selector.unregister(client_sock)
client_sock.close()
break
def _create_socket(self):
self.sock = socket.socket()
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 10)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind(('', self.port))
self.sock.setblocking(False)
self.sock.listen(10)
self.responses = {}
return self.sock
示例11: selector
# 需要导入模块: from selectors import DefaultSelector [as 别名]
# 或者: from selectors.DefaultSelector import select [as 别名]
def selector():
sel = DefaultSelector()
sel.register(proc.stdout.channel, EVENT_READ)
while True:
for key, mask in sel.select():
yield
示例12: __init__
# 需要导入模块: from selectors import DefaultSelector [as 别名]
# 或者: from selectors.DefaultSelector import select [as 别名]
class Loop:
"""
Событийный цикл, который использует очередь сдвухсторонним доступом
Цикл опрашивает селектор на предмет готовности событий для чтения и записи
в сокет
"""
def __init__(self):
"""
Конструктор событийного цикла, который хранит очередь
:return:
"""
self.ready = deque()
self.selector = DefaultSelector()
async def sock_recv(self, sock, max_bytes):
"""
курутина для чтения данных из сокета в асинхронный способ
:param sock: дескриптор сокета
:param max_bytes: максимальное количество байт, который могут быть
прочитаны за один раз без блокировки
:return: принятые из сокета данные в банарном виде
"""
await read_wait(sock)
return sock.recv(max_bytes)
async def sock_accept(self, sock):
await read_wait(sock)
return sock.accept()
async def sock_sendall(self, sock, data):
while data:
try:
n_sent = sock.send(data)
data = data[n_sent:]
except BlockingIOError:
await write_wait(sock)
def create_task(self, coro):
self.ready.append(coro)
def run_forever(self):
while True:
while not self.ready:
events = self.selector.select()
for key, _ in events:
self.ready.append(key.data)
self.selector.unregister(key.fileobj)
while self.ready:
self.current_task = self.ready.popleft()
try:
# запускаем генератор до появления yield
op, *args = self.current_task.send(None)
getattr(self, op)(*args)
except StopIteration:
pass
def read_wait(self, sock):
self.selector.register(sock, EVENT_READ, self.current_task)
def write_wait(self, sock):
self.selector.register(sock, EVENT_WRITE, self.current_task)
示例13: SimpleServer
# 需要导入模块: from selectors import DefaultSelector [as 别名]
# 或者: from selectors.DefaultSelector import select [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
示例14: Main
# 需要导入模块: from selectors import DefaultSelector [as 别名]
# 或者: from selectors.DefaultSelector import select [as 别名]
class Main(object):
def __init__(self, fb):
assert isinstance(fb, Framebuffer)
self._fb = fb
# get all input devices
devices = [InputDevice(fn) for fn in evdev.list_devices()]
# filter out non key devices
for device in devices.copy():
cap = device.capabilities()
if ecodes.EV_KEY not in cap:
devices.remove(device)
continue
self._selector = DefaultSelector()
# This works because InputDevice has a `fileno()` method.
for device in devices:
self._selector.register(device, EVENT_READ)
def _color565(self, r, g, b):
"""Convert red, green, blue components to a 16-bit 565 RGB value. Components
should be values 0 to 255.
"""
return (((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3))
def _img_to_rgb565_bytes(self):
pixels = [self._color565(r, g, b) for (r, g, b) in self._img.getdata()]
return pack('H' * len(pixels), *pixels)
def _write_image(self, img):
if (1 == self._fb.bits_per_pixel):
image_bytes = img.tobytes("raw", "1;IR", self._fb.line_length)
else:
self._img = img
image_bytes = self._img_to_rgb565_bytes()
self._fb.write_raw(image_bytes)
def _do_countdown(self):
if (1 == self._fb.bits_per_pixel):
img = Image.new("1", self._fb.resolution, 1)
else:
img = Image.new("RGB", self._fb.resolution, 'black')
fnt = ImageFont.truetype(font="/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", size=96)
d = ImageDraw.Draw(img)
for i in range(3, 0, -1):
# clear the image
d.rectangle(((0, 0), self._fb.resolution), fill=1)
# draw the text
text_width, text_height = d.textsize(str(i), font=fnt)
x = (self._fb.resolution.x - text_width) / 2
y = (self._fb.resolution.y - text_height) / 2
d.text((x, y), str(i), font=fnt)
self._write_image(img)
# give some time to read
time.sleep(1.25)
def _draw_text(self, text, size):
if (1 == self._fb.bits_per_pixel):
img = Image.new("1", self._fb.resolution, 1)
else:
img = Image.new("RGB", self._fb.resolution, 'black')
fnt = ImageFont.truetype(font="/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", size=size)
d = ImageDraw.Draw(img)
text_width, text_height = d.textsize(text, font=fnt)
x = (self._fb.resolution.x - text_width) / 2
y = (self._fb.resolution.y - text_height) / 2
d.text((x, y), text, font=fnt)
self._write_image(img)
def _take_picture(self):
args = 'fswebcam --quiet --no-banner'
args += ' --scale {0}x{1}'.format(self._fb.resolution.x, self._fb.resolution.y)
if self._fb.grayscale or self._fb.bits_per_pixel == 1:
args += ' --greyscale'
args += ' --png --save {0}'.format(self._filename)
fswebcam = Popen(args, shell=True)
fswebcam.wait()
def _show_picture(self):
if (1 == self._fb.bits_per_pixel):
self._write_image(ImageMath.eval('convert(img, "1")', img=Image.open(self._filename)))
else:
self._write_image(img=Image.open(self._filename))
def run(self):
self._draw_text('Ready!', 36)
# TODO: listen for keypresses on a background thread, otherwise they pile up while taking a picture.
while True:
for key, mask in self._selector.select():
device = key.fileobj
for event in device.read():
if event.type != ecodes.EV_KEY:
# ignore non-key events
continue
if not event.value:
# ignore key up events
continue
if event.code in [ecodes.KEY_ENTER, ecodes.KEY_CAMERA]:
# enter button or camera button takes a picture
#.........这里部分代码省略.........
示例15: Task
# 需要导入模块: from selectors import DefaultSelector [as 别名]
# 或者: from selectors.DefaultSelector import select [as 别名]
# a task get a generator
# then call next to execute the first part, before yied
Task(get('/?q=python+socket+&t=lm&ia=about'))
Task(get('/?q=golang+socket+&t=lm&ia=about'))
Task(get('/?q=rust+socket+&t=lm&ia=about'))
Task(get('/?q=erlang+socket+&t=lm&ia=about'))
Task(get('/?q=python+socket+&t=lm&ia=about'))
Task(get('/?q=golang+socket+&t=lm&ia=about'))
Task(get('/?q=rust+socket+&t=lm&ia=about'))
Task(get('/?q=erlang+socket+&t=lm&ia=about'))
Task(get('/?q=python+socket+&t=lm&ia=about'))
Task(get('/?q=golang+socket+&t=lm&ia=about'))
Task(get('/?q=rust+socket+&t=lm&ia=about'))
Task(get('/?q=erlang+socket+&t=lm&ia=about'))
Task(get('/?q=python+socket+&t=lm&ia=about'))
Task(get('/?q=golang+socket+&t=lm&ia=about'))
Task(get('/?q=rust+socket+&t=lm&ia=about'))
Task(get('/?q=erlang+socket+&t=lm&ia=about'))
while n_tasks:
# retrieve events
events = selector.select()
for event, mask in events:
future = event.data # retrieve the future
# call the callbacks associated to the future
future.resolve()
# get launched serially, so final time is time for one request times number of requests
print("took %.1f sec" % (time.time() - start))