python的I/O多路复用

I/O通信有四种情况:

1,阻塞I/O
2,非阻塞I/O
3, 同步I/O
4,异步I/O

四种I/O模式服务端

#########################################blocking IO
import socket

sk=socket.socket()

sk.bind(("127.0.0.1",8080))

sk.listen(5)

while 1:
    conn,addr=sk.accept()

    while 1:
        conn.send("hello client".encode("utf8"))
        data=conn.recv(1024)
        print(data.decode("utf8"))

########################################################nonblocking IO

import time
import socket
sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sk.bind(('127.0.0.1',6667))
sk.listen(5)
sk.setblocking(False)
print ('waiting client connection .......')
while True:
    try:

        connection,address = sk.accept()   # 进程主动轮询
        print("+++",address)
        client_messge = connection.recv(1024)
        print(str(client_messge,'utf8'))
        connection.close()
    except Exception as e:
        print (e)
        time.sleep(4)

########################################################io多路复用

import socket
import select
sk=socket.socket()
sk.bind(("127.0.0.1",9904))
sk.listen(5)
inp=[sk,]
while True:

    r,w,e=select.select(inp,[],[],5) #[sk,conn]

    for i in r:#[sk,]
        conn,add=i.accept()
        print(conn)
        print("hello")
        inp.append(conn)
    print('>>>>>>')


#***********************异步server.py###################
import socket
import select
sk=socket.socket()
sk.bind(("127.0.0.1",8801))
sk.listen(5)
inputs=[sk,]
while True:
    r,w,e=select.select(inputs,[],[],5)

    for obj in r:#[sk,]
        if obj==sk:
            conn,add=obj.accept()
            print(conn)
            inputs.append(conn)
        else:
            data_byte=obj.recv(1024)
            print(str(data_byte,'utf8'))
            inp=input('回答%s号客户>>>'%inputs.index(obj))
            obj.sendall(bytes(inp,'utf8'))

    print('>>',r)

客户端

#########################################blocking IO
import socket

sk=socket.socket()

sk.connect(("127.0.0.1",8080))

while 1:
    data=sk.recv(1024)
    print(data.decode("utf8"))
    sk.send(b"hello server")


########################################################nonblocking IO
import time
import socket
sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

while True:
    sk.connect(('127.0.0.1',6667))
    print("hello")
    sk.sendall(bytes("hello","utf8"))
    time.sleep(2)
    break

########################################################io多路复用
import socket

sk=socket.socket()

sk.connect(("127.0.0.1",9904))

while 1:
    inp=input(">>").strip()
    sk.send(inp.encode("utf8"))
    data=sk.recv(1024)
    print(data.decode("utf8"))
###############################################异步client.py
import socket

sk = socket.socket()
sk.connect(('127.0.0.1', 8801))

while True:
    inp = input(">>>>")
    sk.sendall(bytes(inp, "utf8"))
    data = sk.recv(1024)
    print(str(data, 'utf8'))
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中的频分多路复用是通过使用select、poll或epoll等函数来实现的。这些函数允许程序同时监视多个文件描述符的可读、可写或错误事件,从而实现对多个I/O通道的高效管理。 频分多路复用的主要优点是可以在单个线程中同时处理多个I/O操作,从而提高程序的并发性能。它适用于需要同时监听多个网络连接或文件描述符的场景,常见的应用包括网络服务器、聊天程序等。 在Python中,可以使用select模块提供的select函数来实现频分多路复用。该函数接受三个参数:可读文件描述符列表、可写文件描述符列表和错误文件描述符列表,然后返回就绪的文件描述符列表。 以下是一个简单的示例代码,演示了如何使用select函数实现频分多路复用: ```python import select import socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(('localhost', 8000)) server_socket.listen(5) inputs = [server_socket] outputs = [] while True: readable, writable, exceptional = select.select(inputs, outputs, inputs) for sock in readable: if sock is server_socket: client_socket, client_address = server_socket.accept() inputs.append(client_socket) else: data = sock.recv(1024) if data: print(data.decode()) if sock not in outputs: outputs.append(sock) else: inputs.remove(sock) if sock in outputs: outputs.remove(sock) sock.close() for sock in writable: sock.send(b'Hello, client!') outputs.remove(sock) for sock in exceptional: inputs.remove(sock) if sock in outputs: outputs.remove(sock) sock.close() ``` 在上述示例中,通过select.select函数来同时监听可读、可写和错误事件。当有新的客户端连接时,会将其加入到inputs列表中。读事件发生时,从客户端接收数据并将其输出。写事件发生时,向客户端发送数据。异常事件发生时,关闭连接。 总之,Python提供了一些函数和模块来实现频分多路复用,能够方便地处理多个I/O通道的并发操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值