Python协程

Python协程



import gevent, time
from urllib import request
from gevent import monkey
monkey.patch_all()

def browser(url, i):
    data = ''
    try:
        for n in range(10):
            with request.urlopen(url) as f:
                data = f.read()
            time.sleep(0.1)
            print('%d %d' % (n, i))
    finally:
        gevent.sleep(0)

tasks = []

for i in range(3000):
    tasks.append( gevent.spawn(browser, 'http://10.10.17.45/', i) )

gevent.joinall(tasks)