협정파충류

파충류의 간단한 실례from urllib import request def (url): print('GET:%s'% url) resp = request.urlopen(url)     data = resp.read()     f=open("url.html",'wb')     f.write(data)     f.close()     print('%d bytes received from %s.' % (len(data), url)) f("https://www.cnblogs.com/alex3714/articles/5248247.html")
 
 
from urllib import request
import gevent,time
from gevent import monkey
monkey.patch_all()
# IO sleepIO

def f(url):
   
print('GET: %s' % url)
    resp = request.urlopen(url)
    data = resp.read()
   
print('%d bytes received from %s.' % (len(data), url))

urls=[
'https://www.python.org/',
     
'https://www.qq.com/',
     
'https://github.com/']

#
start_time=time.time()
for url in urls:
    f(url)
print(" cost:",time.time()-start_time)

#
async_time_start=time.time()

# 3f, f
gevent.joinall([
        gevent.spawn(f,
'https://www.python.org/'),
        gevent.spawn(f,
'https://www.qq.com/'),
        gevent.spawn(f,
'https://github.com/'),
])

print(" cost:",time.time()-async_time_start)

좋은 웹페이지 즐겨찾기