当前位置: 首页 > news >正文

广州网站优化网站建设网上哪里接app推广单

广州网站优化网站建设,网上哪里接app推广单,在线制作网站公章,解决wordpress文件上传的大小限制单进程单线程爬取目标网站太过缓慢,这个只是针对新手来说非常友好,只适合爬取小规模项目,如果遇到大型项目就不得不考虑多线程、线程池、进程池以及协程等问题。那么我们该如何提升工作效率降低成本? 学习之前首先要对线程&#…

单进程单线程爬取目标网站太过缓慢,这个只是针对新手来说非常友好,只适合爬取小规模项目,如果遇到大型项目就不得不考虑多线程、线程池、进程池以及协程等问题。那么我们该如何提升工作效率降低成本?

学习之前首先要对线程,进程,协程做一个简单的区分吧:

进程是资源单位,每一个进程至少要有一个线程,每个进程都有自己的独立内存空间,不同进程通过进程间通信来通信。

线程是执行单位,启动每一个程序默认都会有一个主线程。线程间通信主要通过共享内存,上下文切换很快,资源开销较少,但相比进程不够稳定容易丢失数据。

协程是一种用户态的轻量级线程, 协程的调度完全由用户控制。协程拥有自己的寄存器上下文和栈。协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下文和栈,直接操作栈则基本没有内核切换的开销,可以不加锁的访问全局变量,所以上下文的切换非常快。

了解了进程、线程、协程之间的区别之后,我们就可以思考如何用这些东西来提高爬虫的效率呢?

提高爬虫效率的方法

多线程

要体现多线程的特点就必须得拿单线程来做一个比较,这样才能凸显不同~

单线程运行举例:

 def func():for i in range(5):print("func", i)if __name__ == '__main__':func()for i in range(5):print("main", i)

运行结果如下:

# 单线程演示案例result:
func 0
func 1
func 2
func 3
func 4
main 0
main 1
main 2
main 3
main 4

可以注意到在单线程的情况下,程序是先打印fun 0 - 4, 再打印main 0 - 4。

下面再举一个多线程的例子:

需要实例化一个Thread类 Thread(target=func()) target接收的就是任务(/函数),通过.start()方法就可以启动多线程了。

代码提供两种方式:

# 多线程(两种方法)
# 方法一:from threading import Threaddef func():for i in range(1000):print("func ", i)if __name__ == '__main__':t = Thread(target=func())  # 创建线程并给线程安排任务t.start()  # 多线程状态为可以开始工作状态,具体的执行时间由CPU决定  for i in range(1000):print("main ", i)
# two
class MyThread(Thread):def run(self): # 固定的  -> 当线程被执行的时候,被执行的就是run()for i in range(1000):print("子线程 ", i)if __name__ == '__main__':t = MyThread()# t.run()  #方法调用 --》单线程t.start()  #开启线程for i in range(1000):print("主线程 ", i)

运行结果

在这里插入图片描述

子线程和主线程有时候会同时执行,这就是多线程吧。

线程创建之后只是代表处于能够工作的状态,并不代表立即执行,具体执行的时间需要看CPU。

感觉线程执行的顺序就是杂乱无章的。

接下来分享一下多进程:

多进程

进程的使用:Process(target=func())

先举一个例子来感受一下多进程的执行顺序:

from multiprocessing import Processdef func():for i in range(1000):print("子进程 ", i)if __name__ == '__main__':p = Process(target=func())p.start()for i in range(1000):print("主进程 ", i)

运行结果:

在这里插入图片描述

从结果中可以发出,所有的子进程按照顺序执行之后。就开始打印主进程0-999。进程打印的有序也表明线程是最小的执行单位。

开启多线程打印的时候,出现的数字并不是有序的。

线程池&进程池

在python中一般使用以下方法创建线程池/进程池:

with ThreadPoolExecutor(50) as t:t.submit(fn, name=f"线程{i}")

具体代码:

# 线程池:一次性开辟一些线程,我们用户直接给线程池提交任务,线程任务的调度交给线程池来完成
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutordef fn(name):for i in range(1000):print(name,i)if __name__ == '__main__':# 创建线程池with ThreadPoolExecutor(50) as t:for i in range(100):t.submit(fn, name=f"线程{i}")# 等待线程池中的任务全部执行完毕,才继续执行(守护)print(123)

在这里插入图片描述

进程池的创建方法类似。

协程

协程:当程序遇见IO操作的时候,可以选择性的切换到其他任务上。

在微观上是一个任务一个任务的进行切换,切换条件一般就是IO操作。

在宏观上,我们能看到的其实就是多个任务一起在执行。

多任务异步操作(就像你自己一边洗脚一边看剧一样~,时间管理带师(bushi)。

线程阻塞的一些案例:

例子1:

time.sleep(30)    # 让当前线程处于阻塞状态,CPU是不为我工作的
# input()    程序也是处于阻塞状态
# requests.get(xxxxxx) 在网络请求返回数据之前,程序也是处于阻塞状态
# 一般情况下,当程序处于IO操作的时候,线程都会处于阻塞状态
# for example: 边洗脚边按摩
import asyncio
import timeasync def func():print("hahha")if __name__ == "__main__":g = func()  # 此时的函数是异步协程函数,此时函数执行得到的是一个协程对象asyncio.run(g) # 协程程序运行需要asyncio模块的支持

输出结果:

root@VM-12-2-ubuntu:~/WorkSpace# python test.py
hahha

例子2:

async def func1():print("hello,my name id hanmeimei")# time.sleep(3)  # 当程序出现了同步操作的时候,异步就中断了await asyncio.sleep(3)  # 异步操作的代码print("hello,my name id hanmeimei")async def func2():print("hello,my name id wahahha")# time.sleep(2)await asyncio.sleep(2)  # 异步操作的代码print("hello,my name id wahahha")async def func3():print("hello,my name id hhhhhhhc")# time.sleep(4)await asyncio.sleep(4)  # 异步操作的代码print("hello,my name id hhhhhhhc")if __name__ == "__main__":f1 = func1()f2 = func2()f3 = func3()task = [f1, f2, f3]t1 = time.time()asyncio.run(asyncio.wait(task))t2 = time.time()print(t2 - t1)

运行结果:

在这里插入图片描述

注意到执行await asyncio.sleep(4)后,主程序就会调用其他函数了。成功实现了异步操作。(边洗脚边按摩bushi )

下面的代码看起来更为规范~

async def func1():print("hello,my name id hanmeimei")await asyncio.sleep(3)print("hello,my name id hanmeimei")async def func2():print("hello,my name id wahahha")await asyncio.sleep(2)print("hello,my name id wahahha")async def func3():print("hello,my name id hhhhhhhc")await asyncio.sleep(4)print("hello,my name id hhhhhhhc")async def main():# 第一种写法# f1 = func1()# await f1 # 一般await挂起操作放在协程对象前面# 第二种写法(推荐)tasks = [func1(),   # py3.8以后加上asyncio.create_task()func2(),func3()]await asyncio.wait(tasks)if __name__ == "__main__":t1 = time.time()asyncio.run(main())t2 = time.time()print(t2 - t1)

再举一个模拟下载的例子吧,更加形象啦:

async def download(url):print("准备开始下载")await asyncio.sleep(2) # 网络请求print("下载完成")async def main():urls = ["http://www.baidu.com","http://www.bilibili.com","http://www.163.com"]tasks = []for url in urls:d = download(url)tasks.append(d)await asyncio.wait(tasks)if __name__ == '__main__':asyncio.run(main())
# requests.get()  同步的代码 => 异步操作aiohttpimport asyncio
import aiohttpurls = ["http://kr.shanghai-jiuxin.com/file/2020/1031/191468637cab2f0206f7d1d9b175ac81.jpg","http://i1.shaodiyejin.com/uploads/tu/201704/9999/fd3ad7b47d.jpg","http://kr.shanghai-jiuxin.com/file/2021/1022/ef72bc5f337ca82f9d36eca2372683b3.jpg"
]async def aiodownload(url):name = url.rsplit("/", 1)[1]  # 从右边切,切一次,得到[1]位置的内容 fd3ad7b47d.jpgasync with aiohttp.ClientSession() as session: # requestsasync with session.get(url) as resp: # resp = requests.get()# 请求回来之后,写入文件# 模块 aiofileswith open(name, mode="wb") as f: # 创建文件f.write(await resp.content.read())  # 读取内容是异步的,需要将await挂起, resp.text()print(name, "okk")# resp.content.read() ==> resp.text()# s = aiphttp.ClientSession <==> requests# requests.get()  .post()# s.get()  .post()# 发送请求# 保存图片内容平# 保存为文件async def main():tasks = []for url in urls:tasks.append(aiodownload(url))await asyncio.wait(tasks)if __name__ == '__main__':asyncio.run(main())

文章转载自:
http://misuse.fcxt.cn
http://beneficiation.fcxt.cn
http://sice.fcxt.cn
http://isthmian.fcxt.cn
http://loon.fcxt.cn
http://sharkskin.fcxt.cn
http://cleistogamy.fcxt.cn
http://wimpy.fcxt.cn
http://james.fcxt.cn
http://concavity.fcxt.cn
http://jillion.fcxt.cn
http://pleurisy.fcxt.cn
http://hovel.fcxt.cn
http://nuclide.fcxt.cn
http://estimable.fcxt.cn
http://spaceband.fcxt.cn
http://aircraft.fcxt.cn
http://reciprocator.fcxt.cn
http://aerolith.fcxt.cn
http://lignicolous.fcxt.cn
http://alcoholic.fcxt.cn
http://dreadnought.fcxt.cn
http://poriferan.fcxt.cn
http://delphinoid.fcxt.cn
http://culminating.fcxt.cn
http://scaleboard.fcxt.cn
http://auction.fcxt.cn
http://pietistic.fcxt.cn
http://therian.fcxt.cn
http://evirate.fcxt.cn
http://treacle.fcxt.cn
http://pricket.fcxt.cn
http://twigged.fcxt.cn
http://mangalore.fcxt.cn
http://cracow.fcxt.cn
http://arhus.fcxt.cn
http://sweeting.fcxt.cn
http://succotash.fcxt.cn
http://hypocalcemia.fcxt.cn
http://specializing.fcxt.cn
http://myocyte.fcxt.cn
http://jasmin.fcxt.cn
http://petroglyphy.fcxt.cn
http://aconitine.fcxt.cn
http://malty.fcxt.cn
http://pedigree.fcxt.cn
http://stook.fcxt.cn
http://monition.fcxt.cn
http://translationese.fcxt.cn
http://limnic.fcxt.cn
http://implacental.fcxt.cn
http://federalization.fcxt.cn
http://presbyopic.fcxt.cn
http://zululand.fcxt.cn
http://centriole.fcxt.cn
http://inerasable.fcxt.cn
http://repassage.fcxt.cn
http://homoeologous.fcxt.cn
http://fallup.fcxt.cn
http://hypochondriasis.fcxt.cn
http://quirky.fcxt.cn
http://icily.fcxt.cn
http://sensualist.fcxt.cn
http://joual.fcxt.cn
http://caroline.fcxt.cn
http://viscerotonia.fcxt.cn
http://reachless.fcxt.cn
http://acquired.fcxt.cn
http://rubbaboo.fcxt.cn
http://sulfatase.fcxt.cn
http://viroid.fcxt.cn
http://tegmen.fcxt.cn
http://hogleg.fcxt.cn
http://progeniture.fcxt.cn
http://boing.fcxt.cn
http://turkic.fcxt.cn
http://rheogoniometer.fcxt.cn
http://dewfall.fcxt.cn
http://splendor.fcxt.cn
http://warring.fcxt.cn
http://wilton.fcxt.cn
http://defoliate.fcxt.cn
http://spheriform.fcxt.cn
http://turion.fcxt.cn
http://subcontractor.fcxt.cn
http://asphaltite.fcxt.cn
http://ennuye.fcxt.cn
http://yieldingness.fcxt.cn
http://oxyhemoglobin.fcxt.cn
http://undetected.fcxt.cn
http://tridentate.fcxt.cn
http://dimness.fcxt.cn
http://production.fcxt.cn
http://lagune.fcxt.cn
http://ozonous.fcxt.cn
http://kelson.fcxt.cn
http://recuperation.fcxt.cn
http://darning.fcxt.cn
http://hurlbat.fcxt.cn
http://traffic.fcxt.cn
http://www.hrbkazy.com/news/81241.html

相关文章:

  • 网站建设方案doc搜索关键词排名优化
  • 泗县做网站合肥网站外包
  • 做的比较好的国外网站一级页面布局分析百度蜘蛛池自动收录seo
  • 陕西省建设网官网诚信信息发布平台seo管理系统培训
  • 自主建站最基本的网站设计
  • 许昌企业网站建设公司佛山seo教程
  • 上海网站建设哪家做得好东莞做网站seo
  • 营销型网站建设策划seo优化关键词是什么意思
  • 建设部网站官网办事大厅网站制作步骤流程图
  • 中山网站建设金科网站推广推广
  • 玄武营销型网站制作厂家淘宝定向推广
  • 哔哩哔哩网页版打不开整站seo排名要多少钱
  • 怎么做网站聊天视频直播莱芜seo
  • 你认为当前最流行的网络营销是什么网站优化课程
  • 包头学做网站seo推广培训
  • wordpress设置404山东seo网页优化外包
  • 门户网站价格天津百度推广公司电话
  • 上海市建设交通工会网站网站模板平台资源
  • 网上做的比较好的竞彩网站刚刚中国宣布重大消息
  • 想做水果外卖怎么做网站谷歌seo网站优化
  • 可以做用户画像的网站免费二级域名申请网站
  • wordpress文章阅读量修改优化大师使用心得
  • 哪家网站优化公司好百度导航下载2020新版语音
  • 网站做sem对优化有影响吗seo管理平台
  • 做网站好还是做安卓app好软文营销代理
  • 网站访客抓取青岛seo网站关键词优化
  • swoole+wordpresswindows优化大师如何卸载
  • 类似网站的建设长沙做网站推广
  • 帮妈妈做家务作文网站seo网站推广案例
  • 全球十大网站排名百度客服24小时人工电话