python进阶 IO多路复用

  • 可以监听多个文件描述符(socket对象)(文件句柄),一旦文件句柄出现变化,即可感知
    # 服务端
    import socket
    
    sk1 = socket.socket()
    sk1.bind(('127.0.0.1', 8001, ))
    sk1.listen()
    
    sk2 = socket.socket()
    sk2.bind(('127.0.0.1', 8002, ))
    sk2.listen()
    
    sk3 = socket.socket()
    sk3.bind(('127.0.0.1', 8003, ))
    sk3.listen()
    
    inputs = [sk1, sk2, sk3, ]
    import select
    while True:
        # r_list ->inputs 监听变化, w_list -> [sk1, sk2] 有什么就放如什么
        # e_list -> [] 对相应错误,1:等待的时间
        r_list, w_list, e_list = select.select(inputs, [sk1, sk2], inputs, 1)
        for sk in r_List:
            # print(sk)
            # 每一个连接对象
            conn, address = sk.accept()
            conn.sendall(bytes('hello', encoding='utf-8'))
            # conn.close()
        for sk in e_list:
            # 哪一个连接出错就移除它
            inputs.remove(sk)
    # ---------------------------------------------------------------------------
    # 客户端1
    import socket
    
    obj = socket.socket()
    obj.connect(('127.0.0.1', 8001))
    
    content = str(obj.recv(1024), encoding='utf-8')
    print(content)
    obj.close()
    # --------------------------------------------------------------------------
    # 客户端2
    import socket
    
    obj = socket.socket()
    obj.connect(('127.0.0.1', 8002))
    
    content = str(obj.recv(1024), encoding='utf-8')
    print(content)
    obj.close()
    
  • 基于IO多路复用select+socket实现伪并发
    # 服务端
    import socket
    
     sk1 = socket.socket()
     sk1.bind(('127.0.0.1', 8001, ))
     sk1.listen()
    
     inputs = [sk1,  ]
     outputs = []
     message_dict = {}
    
     import select
     while True:
         r_list, w_list, e_list = select.select(inputs, outputs, inputs, 1)
         print('正在监听的socket对象有%d个', len(inputs))
         print(r_list)
         for sk1_or_conn in r_List:
             # 每一个连接对象
             if sk1_or_conn == sk1:
                 # 表示有新用户来连接
                 conn, address = sk1_or_conn.accept()
                 inputs.append(conn)
                 message_dict[conn] = []
             else:
                 # 有老用户发消息了
                 try:
                     data_bytes = sk1_or_conn.recv(1024)
                 except Exception as e:
                     inputs.remove(sk1_or_conn)
                 else:
                      data_str = str(data_bytes, encoding='utf-8')
                      message_dict[sk1_or_conn].append(data_str)
          # w_list仅仅保存了谁给我发过消息
          for conn in w_list:
              recv_str = message_dict[conn][0]
              del message_dict[conn][0]
              conn.sendall(bytes(recv_str + '好', encoding='utf-8'))
              outputs.remove(conn)
          for sk in e_list:
              inputs.remove(sk)
     # -------------------------------------------------------------------------
     # 客户端
     import socket
    
     obj = socket.socket()
     obj.connect(('127.0.0.1', 8001))
    
     while True:
         inp = input('>>>>')
         obj.sendall(bytes(inp, encoding='utf-8'))
         ret = str(obj.recv(1024), encoding='utf-8')
         print(ret)
    
    obj.close()
    

你可能感兴趣的:(python进阶 IO多路复用)