Python select sets the timeout. After a while, it does not return.

I am writing a web server. Centos, interacts with multiple clients and uses the select model. After running for a period of time, the program suddenly gets stuck in select. It is clear that socket is in establish status, and there should be packets on it, but select does not return. Not sure if there is something wrong with the select mechanism or something else. Code:

ip="x.x.x.x"
port1 = 8322
server = socket.socket()
server.setblocking(False)
server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
server.bind((ip,port1));
server.listen(10)
cli_socket = socket.socket()
cli_socket.setblocking(False)
host = "127.0.0.1"
port2 = 12355
cli_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
cli_socket.bind((host, port2))
cli_socket.listen(5)
inputs=[server,cli_socket,]
while True:
readable, writable, exceptional = select.select(inputs, outputs, inputs, 30)
for s in readable:
    if s is server:
        conn,address = server.accept()
        rec_buf=conn.recv(1024)
        //make some handle here, store this connection in global data
        conn.setblocking(0)
        conn.sendall("pass")
        inputs.append(conn)
    elif s is cli_socket:
        cli_conn, addr = s.accept()
        command = cli_conn.recv(1024)
        ret=command.split(",")
            if ret[0] == "OPEN":                                                
                total_key[ret[1]].conn_sock.sendall("open")
                cli_conn.send("success")
                cli_conn.close()
    else:
        rec_buf=s.recv(1024)                                        
        //make some handle here                        
        s.sendall("ok")
for s in exceptional:
    inputs.remove(s)
    s.close()
Apr.03,2021

has many minor problems, such as

1        conn,address = server.accept()
2        rec_buf=conn.recv(1024)
3        //make some handle here, store this connection in global data
4        conn.setblocking(0)
5        conn.sendall("pass")

Line 2 blocks the entire thread until the other party sends data, or the connection is disconnected.
Line 5 may fail because it does not prejudge whether conn is writable.

it is recommended to use asyncio rewriting. Refer to

.
-sharp -*- coding: utf-8 -*-
import asyncio


async def handle_server(reader, writer):
    data = await reader.readexactly(10)
    print('recv1: {}'.format(data.decode()))
    writer.write('pass'.encode())
    writer.close()


async def handle_client(reader, writer):
    data = await reader.readexactly(4)
    if data.decode() == 'OPEN':
        writer.write('success'.encode())
    writer.close()


def new_listener(loop, ip, port, handler):
    coro = asyncio.start_server(handler, ip, port, loop=loop)
    return loop.run_until_complete(coro)


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    new_listener(loop, '127.0.0.1', 8322, handle_server),
    new_listener(loop, '127.0.0.1', 12355, handle_client),
    loop.run_forever()
Menu