1. 导gevent中的猴子补丁,来把原来python自带的socket变成基于epoll的socket(解除阻塞问题)
代码:
# from gevent import monkey;monkey.patch_socket() #猴子补丁,把我们python自带的socket替换掉,实现了一个封装了epoll的socket
from gevent import monkey
monkey.patch_socket()
import socket
import gevent
server = socket.socket() #封装了epoll的套接字,可避开阻塞
server.bind(('127.0.0.1',7788))
server.listen(1000)
def worker(conn):
data = conn.recv(1024)
if data:
print(data.decode('utf8'))
conn.send(data)
else:
conn.close()
while True:
conn,addr = server.accept()
gevent.spawn(worker,conn)
图例: