python gevent并发socket

松鼠的窝 2018-05-05

import queue
import gevent
from gevent import monkey; monkey.patch_socket()#猴子补丁代替python原有的socket
selector = selectors.DefaultSelector()

socket_server = socket.socket()
address_server = ('0.0.0.0',8080)
socket_server.bind(address_server)
socket_server.listen(1000)
q = queue.Queue(3)

def conmuer(connect):
    while True:
        massage = connect.recv(1024)
        if massage:
            print(massage)
            connect.send(massage)
        else:
            connect.close()
            break

while True:
    connect, address = socket_server.accept()
    print(connect)
    gevent.spawn(conmuer, connect)#创建协程

相关推荐