Day95
#爬虫性能相关
# 1.多线程
# """
# 可以实现并发
# 但是,请求发送出去后和返回之前,中间时期线程空闲
# 编写方式:
# - 直接返回处理
# - 通过回调函数处理
# """
# ########### 编写方式一 ###########
# """
# from concurrent.futures import ThreadPoolExecutor
# import requests
# import time
#
# def task(url):
# response = requests.get(url)
# print(url,response)
# # 写正则表达式
#
#
# pool = ThreadPoolExecutor(7)
# url_list = [
# 'http://www.cnblogs.com/wupeiqi',
# 'http://huaban.com/favorite/beauty/',
# 'http://www.bing.com',
# 'http://www.zhihu.com',
# 'http://www.sina.com',
# 'http://www.baidu.com',
# 'http://www.autohome.com.cn',
# ]
# for url in url_list:
# pool.submit(task,url)
#
# pool.shutdown(wait=True)
# """
#
# ########### 编写方式二 ###########
# from concurrent.futures import ThreadPoolExecutor
# import requests
# import time
#
# def task(url):
# """
# 下载页面
# :param url:
# :return:
# """
# response = requests.get(url)
# return response
#
# def done(future,*args,**kwargs):
# response = future.result()
# print(response.status_code,response.content)
#
# pool = ThreadPoolExecutor(7)
# url_list = [
# 'http://www.cnblogs.com/wupeiqi',
# 'http://huaban.com/favorite/beauty/',
# 'http://www.bing.com',
# 'http://www.zhihu.com',
# 'http://www.sina.com',
# 'http://www.baidu.com',
# 'http://www.autohome.com.cn',
# ]
# for url in url_list:
# v = pool.submit(task,url)
# v.add_done_callback(done)
#
# pool.shutdown(wait=True)
#
##########################多进程
"""
# 可以实现并发
# 但是,请求发送出去后和返回之前,中间时期进程空闲
# 编写方式:
# - 直接返回处理
# - 通过回调函数处理
# """
#
# ########### 编写方式一 ###########
# """
# from concurrent.futures import ProcessPoolExecutor
# import requests
# import time
#
# def task(url):
# response = requests.get(url)
# print(url,response)
# # 写正则表达式
#
#
# pool = ProcessPoolExecutor(7)
# url_list = [
# 'http://www.cnblogs.com/wupeiqi',
# 'http://huaban.com/favorite/beauty/',
# 'http://www.bing.com',
# 'http://www.zhihu.com',
# 'http://www.sina.com',
# 'http://www.baidu.com',
# 'http://www.autohome.com.cn',
# ]
# for url in url_list:
# pool.submit(task,url)
#
# pool.shutdown(wait=True)
# """
#
# ########### 编写方式二 ###########
# from concurrent.futures import ProcessPoolExecutor
# import requests
# import time
#
# def task(url):
# response = requests.get(url)
# return response
#
# def done(future,*args,**kwargs):
# response = future.result()
# print(response.status_code,response.content)
#
# pool = ProcessPoolExecutor(7)
# url_list = [
# 'http://www.cnblogs.com/wupeiqi',
# 'http://huaban.com/favorite/beauty/',
# 'http://www.bing.com',
# 'http://www.zhihu.com',
# 'http://www.sina.com',
# 'http://www.baidu.com',
# 'http://www.autohome.com.cn',
# ]
# for url in url_list:
# v = pool.submit(task,url)
# v.add_done_callback(done)
#
# pool.shutdown(wait=True)
########################################################################################################
# 线程与进程的区别
# 1.一个线程共享进程里的所有资源
# IO密集型用多线程
# 计算密集型用多进程
# 应用程序就是一个软件,一个软件就是一个进程,进程可以包含多个线程
# Pyhton里有JIL锁,同一时刻只能有一个线程会被CPU调度,如果用线程会被限制,即使开了多线程,效率反而不高
# 对于IO操作是不用通过CPU的
#
# 并发
# 异步IO
# 协程:一个线程在做很多事情,让一个线程先执行一个函数,执行时终止再执行其他,都是仅仅是切换
# 协程与异步IO可以共同完成并发请求
# Python内置模块:asyncio
Win a contest, win a challenge
posted on 2018-06-15 06:14 pandaboy1123 阅读(142) 评论(0) 编辑 收藏 举报