示例#1
0
def main(num=1000, forever=False):
    while True:
        gls = []
        for i in xrange(num):
            while True:
                try:
                    s = socket()
                except pysocket.error, e:
                    if e.errno != 24:
                        raise
                    ioloop.coreloop().switch()
                else:
                    break
            s.connect(("127.0.0.1", 2424))
                
            gl = greenlet.greenlet(recvall)
            gls.append((i, s, gl))
            gl.switch(s, i)
        
        while gls:
            for i, s, gl in gls:
                if gl.dead:
                    gls.remove((i, s, gl))
                else:
                    gl.switch()

        if not forever:
            break
示例#2
0
def main():
    i = 0
    
    s = socket.socket(pysocket.AF_INET, pysocket.SOCK_STREAM)
    s.setsockopt(pysocket.SOL_SOCKET, pysocket.SO_REUSEADDR, 1)
    s.bind(('0.0.0.0', 2424))
    try:
        s.listen(5)
        while go:
            try:
                c, a = s.accept()
                g = connection(i, c, a)
                g.switch()
                i += 1
            except:
                pass
    finally:
        s.shutdown(pysocket.SHUT_RDWR)
        s.close()
    log.info("Server stopped")