码迷,mamicode.com
首页 > 其他好文 > 详细

selectors实现高并发

时间:2016-09-30 21:07:47      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

1. 下面的例子,客户端给服务端发送消息,服务端把消息返回

server

#!/usr/bin/env python


import selectors
import socket
import time
import threading


sel = selectors.DefaultSelector()

def echo(conn,cmd,filename):
    time.sleep(3)
    conn.send((cmd +   + filename).encode())

def accept_client(sock,mask):
    conn,addr = sock.accept()
    print(conn:{},addr:{},mask:{}.format(conn,addr,mask))
#     conn.setblocking(False)
    sel.register(conn, selectors.EVENT_READ, read_client)

def read_client(conn,mask):
    data = conn.recv(1024).decode().strip()
    if data:
        if len(data.split( )) == 2:
            cmd = data.split( )[0]
            filename = data.split( )[1]
            t = threading.Thread(target=echo,args=(conn,cmd,filename))   #这里使用了线程,不然程序会变成串行的,执行一个客户端任务,sleep(3)然后再执行下一个客户端任务...
            t.start()
#             time.sleep(1)
#             conn.send((cmd + ‘ ‘ + filename).encode())
        else:
            conn.send((usage: put filename ;get filename).encode())
    else:
        print({} closed.format(conn))
        sel.unregister(conn)
        conn.close()
server = socket.socket()
server.bind((localhost,1235))
server.listen(12340)
# server.setblocking(False)
sel.register(server, selectors.EVENT_READ,accept_client)


while True:
    events = sel.select()
    for key,mask in events:
        callable = key.data
        callable(key.fileobj,mask)

client

#!/usr/bin/env python

import socket
import threading
import time

ip_port = (localhost,1235)
socks = [socket.socket() for i in range(1000)]        

m = bget abc
time_list = []
def run(s):
    s.connect(ip_port)
    s.send(m)
    data = s.recv(1024)
    print(s,data)
    
start_time = time.time()   
for s in socks:
    t = threading.Thread(target=run,args=(s,))
    t.start()
    time_list.append(t)
for i in time_list:
    i.join()
print(time:{}.format(start_time - time.time()))
    

2. 实现并发put和get文件

 

selectors实现高并发

标签:

原文地址:http://www.cnblogs.com/fuckily/p/5924672.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!