Python协程之测试读写网页时自动切换,实现并发效果,并对比多线程执行。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haeasringnar/article/details/79978630
import time
from gevent import monkey;monkey.patch_all()
import gevent
from  urllib.request import urlopen
import threading

'''
是么是IO操作:
文件读写就是最常见的IO操作,这里的打开url其实就是一个:读url地址里的网页html文件,就是一个抽象的文件读写。
'''
def f(url):
    print('GET: %s' % url)
    resp = urlopen(url)
    data = resp.read()
    print('%d bytes received from %s.' % (len(data), url))

start_time01 = time.time()
# 我们这里使用的协程,gevent就会做到,遇到io操作,自动切换任务,就会节省很多时间
# 实现了在一个线程里面开启并发的效果
gevent.joinall([
    gevent.spawn(f, 'https://www.python.org/'), #这个就是启动一个协程
    gevent.spawn(f, 'https://www.yahoo.com/'),
    gevent.spawn(f, 'https://github.com/'),
])
# gevent.joinall 类似进程池,用来启动多个协程
print('耗费时间为(s):',round(time.time()-start_time01))

# 比较普通用法消耗的时间 异步协程的方法几乎是同步的四分之一
start_time02 = time.time()
def func():
    url_list = ['https://www.python.org/', 'https://www.yahoo.com/','https://github.com/']
    for url in url_list:
        print('GET: %s' % url)
        resp = urlopen(url)
        data = resp.read()
        print('%d bytes received from %s.' % (len(data), url))

func()
print('耗费时间为(s):',round(time.time()-start_time02))

# 下面是开启多线程的模式
start_time03 = time.time()
def read(url):
    print('GET: %s' % url)
    resp = urlopen(url)
    data = resp.read()
    print('%d bytes received from %s.' % (len(data), url))
def func():
    url_list = ['https://www.python.org/', 'https://www.yahoo.com/','https://github.com/']
    t_list = []
    for url in url_list:
        t = threading.Thread(target=read,args=(url,))
        t.start()
        t_list.append(t)
    for j in t_list:
        j.join()
func()
print('耗费时间为(s):',round(time.time()-start_time03))
阅读更多

没有更多推荐了,返回首页