python io多路復用

2022-08-24 03:39:11 字數 2146 閱讀 2420

用for迴圈執行多使用者訪問

sever

import socket

sk1 = socket.socket()

sk1.bind(('127.0.0.1', 888))

sk1.listen()

import select

ipt = [sk1, ]

output =

message_dic={}

eer=

while true:

r_list, w_list, e_list = select.select(ipt, output, eer, 1)

print('正在監聽的sk物件 %s' % len(ipt))

print(r_list)

# for con in w_list:

# con.sendall(bytes('hey', encoding='utf-8'))

# output.remove(con)

for sk_con in r_list:

if sk_con == sk1:

# 表示新客戶來

con, addr = sk_con.accept()

message_dic[con]=

else:

# 老客戶發訊息

try:

data_bytes = sk_con.recv(1024)

# data_str = str(data_bytes, encoding='utf-8')

# sk.sendall(bytes(data_str + 'hao', encoding='utf-8'))

except connectionreseterror:

else:

# 使用者正常發訊息

data_str = str(data_bytes, encoding='utf-8')

# ipt.remove(sk)

# con.sendall(bytes('hi',encoding='utf-8'))

# for sk in e_list:

# ipt.remove(sk)

for cont in w_list:

rec_str=message_dic[cont][0]

cont.sendall(bytes(rec_str+'hhhhh',encoding='utf-8'))

del message_dic[cont][0]

output.remove(cont)

for sk in e_list:

ipt.remove(sk)

sk1=socket.socket()

sk1.bind(('127.0.0.1',888))

sk1.listen()

import select

ipt=[sk1,]

output=

while true:

r_list, w_list, e_list = select.select(ipt, output, ipt, 1)

print('正在監聽的sk物件 %s' % len(ipt))

print(r_list)

for con in w_list:

con.sendall(bytes('hey', encoding='utf-8'))

output.remove(con)

for sk in r_list:

if sk==sk1:

# 表示新客戶來了

con,addr=sk.accept()

else:

# 老客戶發訊息

try:

data_bytes=sk.recv(1024)

data_str=str(data_bytes,encoding='utf-8')

sk.sendall(bytes(data_str+'hao',encoding='utf-8'))

except connectionreseterror:

# ipt.remove(sk)

# con.sendall(bytes('hi',encoding='utf-8'))

# for sk in e_list:

# ipt.remove(sk)

Python IO多路復用select模組

多路復用的分析例項 服務端 客戶端 服務端配置 from socket import import time import select server socket af inet,sock stream server.bind 127.0.0.1 8080 server.listen 5 serv...

python IO多路復用,select模組

觸發機制 1 水平觸發 2 邊緣觸發 io多路復用單執行緒實現併發,實現模組 1 select 效率最低 2 poll 3 epoll 最好,nginx的實現 linux下有這3種模組,windows下只有select模組 io多路復用的好處 同時可以監聽多個連線 io多路復用的select模組觸發...

Linux IO多路復用

一.select 函式 include include include int select int n,fd set readfds,fd set writefds,fd set exceptfds,struct timeval timeout fd clr int fd,fd set set f...