简体   繁体   中英

Gevent vs sync processing for scanning, why are they approximately the same speed?

def cn(host, port):
    try:
        conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        conn.connect((host, port))
        print '[+]%d/tcp open' % port
        conn.close()
    except:
        pass
        #print '[-]%d/tcp closed' % port

def ge():
    st = time.time()

    threads = [gevent.spawn(cn, '127.0.0.1', i) for i in xrange(1000)]
    gevent.joinall(threads)

    print "using gevent - " + str(time.time() - st)

def ss():
    st = time.time()

    for i in range(1, 1000):
        connScan('127.0.0.1', i)

    print "using sync processing - " + str(time.time() - st)

Gevent is just a tad bit faster than sync processing. Why?

Is it possible to improve the above code to make it faster using gevent?

大多数端口是关闭的,因此完全没有阻塞可从gevent版本获得加速。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM