Commit d6c21492 authored by Filip Široký's avatar Filip Široký Committed by Petr Špaček

Use selectors instead of select

parent 1e55bd65
Pipeline #38975 passed with stage
in 1 minute and 34 seconds
...@@ -5,7 +5,7 @@ import itertools ...@@ -5,7 +5,7 @@ import itertools
import logging import logging
import os import os
import signal import signal
import select import selectors
import socket import socket
import sys import sys
import threading import threading
...@@ -126,23 +126,30 @@ class TestServer: ...@@ -126,23 +126,30 @@ class TestServer:
if not self.active: if not self.active:
break break
objects = self.srv_socks + self.connections objects = self.srv_socks + self.connections
to_read, _, to_error = select.select(objects, [], objects, 0.1) sel = selectors.DefaultSelector()
for sock in to_read: for obj in objects:
if sock in self.srv_socks: sel.register(obj, selectors.EVENT_READ)
if sock.proto == socket.IPPROTO_TCP: items = sel.select(0.1)
conn, _ = sock.accept() for key, event in items:
self.connections.append(conn) sock = key.fileobj
if event & selectors.EVENT_READ:
if sock in self.srv_socks:
if sock.proto == socket.IPPROTO_TCP:
conn, _ = sock.accept()
self.connections.append(conn)
else:
self.handle_query(sock)
elif sock in self.connections:
if not self.handle_query(sock):
sock.close()
self.connections.remove(sock)
else: else:
self.handle_query(sock) raise Exception(
elif sock in self.connections: "[query_io] Socket IO internal error {}, exit"
if not self.handle_query(sock): .format(sock.getsockname()))
sock.close()
self.connections.remove(sock)
else: else:
raise Exception( raise Exception("[query_io] Socket IO error {}, exit"
"[query_io] Socket IO internal error {}, exit".format(sock.getsockname())) .format(sock.getsockname()))
for sock in to_error:
raise Exception("[query_io] Socket IO error {}, exit".format(sock.getsockname()))
def start_srv(self, address, family, proto=socket.IPPROTO_UDP): def start_srv(self, address, family, proto=socket.IPPROTO_UDP):
""" Starts listening thread if necessary """ """ Starts listening thread if necessary """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment