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

揭阳做淘宝批发拿货什么网站百度联盟广告

揭阳做淘宝批发拿货什么网站,百度联盟广告,凡科网 小程序,自家电脑做网站服务器w7花生壳免责声明:本文仅做技术交流与学习... 目录 了解进程和线程 单个线程(主线程)在执行 多线程 线程池 协程(爬虫多用) 假异步:(同步) 真异步: 爬虫代码模版 异步-爬虫 同步效果--19秒 异步效果--7秒 了解进程和线程 ​ # --------------------> # ------> # …

免责声明:本文仅做技术交流与学习... 

目录

了解进程和线程

单个线程(主线程)在执行

多线程

线程池

协程(爬虫多用)

假异步:(同步)

真异步:

爬虫代码模版

异步-爬虫

同步效果--19+秒

异步效果--7+秒


了解进程和线程

​
# -------------------->
# ------>
#       ------->
#               -------->
​
# 1-线程
#线程:执行一个软件后的操作---点赞,签到,评论等等
#进程:执行一个软件
​
​
一家公司里面人去做事.
1人,2人,多人...
(要合理分配,合理运用.)
--30万的资本,养不起10000人呀.
​
1个进程必须要有一个线程,--- 线程不是越多越好.
单线程/多线程
​
​
进程:资源单元
线程:执行单元
​
​
​
​
# 每一个py程序默认都有一个线程的,
print("111")
​

单个线程(主线程)在执行


多线程

from threading import Thread
# alt + enter 快捷键导包
​
def func(name):for i in range(1, 1000):print("func函数在执行---" + str(i),name)
​
​
# func()  # 这样写就是主线程执行.
​
# 创建线程对象,分配线程的任务是func   (公司招人要分配任务)
t = Thread(target=func,args=('my name xiaodi',))   # args的参数必须是一个元组.
# 启动线程:                       (员工先忙完手头工作,然后真正工作)
t.start()   # 线程的状态,可以开始工作的状态了,具体的执行时间由CPU决定.
​
t1 = Thread(target=func,args=('xiaosedi',))
t1.start()
​
# 主线程不会受到子线程(其他线程)的干扰.  主线程该干什么就干什么.
for i in range(1, 1000):print("主---" + str(i))
# 多个线程都输出到控制台上,就会乱.
​
# 传参在创建线程对象时也要传.
​
# 线程数由电脑的CPU决定,如果处理不好,反而会效率下降.
​


线程池

# 线程池 :一次性开辟一些线程,直接给线程池提交任务,具体的任务到底哪个线程执行,是由线程池分配的
from concurrent.futures.thread import ThreadPoolExecutor
​
​
def func(name):for i in range(1000):print(name, 'func函数执行', i)
​
​
# 创建一个有50个线程的线程池.(合理的利用资源~)
# -----执行10次func函数,每个func函数执行1000次.
with ThreadPoolExecutor(50) as t:#     t = ThreadPoolExecutor(50)for i in range(10):# 给线程去提交任务t.submit(func, name=f'线程{i}')
​
# 等待线程池中的任务全部执行完毕,才会继续执行
print('print执行了')


协程(爬虫多用)

import asyncio
import time
​
def func():print("函数开始")time.sleep(3)   # 当到此时,当前线程为阻塞状态,CPU不会为当前程序提供工作.print("函数结束")
func()
​
# 阻塞代码:(必须要等待某个结果等等
# input(等待输入)   time.sleep(强制等待)    requests(请求网络,client<->server有时间差,
# 程序基于 i(input) o(output) 操作时,线程机会处于阻塞状态,CPU就不会提供工作.
# ---阻塞的时候就会干等着,---怎么让CPU在干等着的时候也做点事情呢?--->协程!!!
​
# 协程:当程序遇见了io操作的时候,可以选择性的切换到其它任务上。
# 多任务异步操作:

假异步:(同步)

import asyncio
import time
​
​
async def func1():print('func1函数开始')time.sleep(3)  # 属于同步操作代码。# 只要在异步程序中出现了同步操作,异步就被中断# await asyncio.sleep(3)print('func1函数结束')
​
​
async def func2():print('func2函数开始')time.sleep(2)# await asyncio.sleep(2)print('func2函数结束')
​
​
async def func3():print('func3函数开始')time.sleep(4)# await asyncio.sleep(4)print('func3函数结束')
​
​
# 拿到函数的对象
f1 = func1()
f2 = func2()
f3 = func3()
tasks = [# 创建一个任务f1, f2, f3
]
start = time.time()
# 如果是多个任务,需要一个asyncio.wait(任务列表)搭配
asyncio.run(asyncio.wait(tasks))
print(time.time() - start)
​

9+秒结束!!! ---没有异步呀---

因为time是一个同步模块,

time.sleep()  # 属于同步操作代码。
# 只要在异步程序中出现了同步操作,异步就被中断

真异步:

import asyncio
import time
​
​
async def func1():print('func1函数开始')# time.sleep(3)         # 属于同步操作代码await asyncio.sleep(3)  # 异步休眠代码       --不是强制性的休眠,而是挂起,让他先去忙别的东西,等好了再回来.print('func1函数结束')
​
​
async def func2():print('func2函数开始')# time.sleep(2)await asyncio.sleep(2)print('func2函数结束')
​
​
async def func3():print('func3函数开始')# time.sleep(4)await asyncio.sleep(4)print('func3函数结束')#async def main():
#     f1 = func1()
#     f2 = func2()
#     f3 = func3()
#     tasks = [
#         f1,f2,f3
#         # 创建一个任务
#         # asyncio.create_task(func1()),
#         # asyncio.create_task(func2()),
#         # asyncio.create_task(func3())
#     ]
#     await asyncio.wait(tasks)
# start = time.time()
# asyncio.run(main())
# print(time.time() - start)
​
f1 = func1()
f2 = func2()
f3 = func3()
tasks = [f1, f2, f3# 创建一个任务
]
​
start = time.time()
# 如果是多个任务,需要一个asyncio.wait(任务列表)搭配
asyncio.run(asyncio.wait(tasks))
print(time.time() - start)

4+秒 , 好快呀...


爬虫代码模版

import asyncio
​
​
async def download(url):print('准备开始下载')# await asyncio.sleep(2) # 网络请求# requests.get(url)      # 异步效果中断,那怎么结合呢???print('下载完成')
​
​
async def main():urls = ['地址1','地址2','地址3',]# tasks = []# for url in urls:#    tasks.append(download(url))
​# 列表推导式写法 循环url列表,每循环一次,创建一个任务tasks = [download(url) for url in urls]await asyncio.wait(tasks)
​
​
asyncio.run(main())
​

requests.get(url) # 异步效果中断,那怎么结合呢???

只要出现同步操作,异步就会被终断.

-------->


异步-爬虫

因为requests模块是同步的,如果在异步协程中编写同步代码,异步效果没有。
​
如何解决?
更换支持异步的请求模块
aiohttp  == requests
pip install aiohttp
pip install aiofiles

同步效果--19+秒

import time
import requests
​
urls = ['https://www.cgwallpapers.com/wallpapers_free_wreoiux/wallpaper_christian_dimitrov_02_1920x1080.jpg','https://www.cgwallpapers.com/wallpapers_free_wreoiux/wallpaper_pablo_carpio_17_1920x1080.jpg','https://www.cgwallpapers.com/wallpapers_free_wreoiux/wallpaper_dejian_wu_04_1920x1080.jpg'
]
t = time.time()
for url in urls:res = requests.get(url).content# 文件名name = url.split('/')[-1]with open(name, 'wb') as f:f.write(res)
print(f'requests花费时间===》{time.time() - t}')
# requests花费时间===》19.635247230529785

异步效果--7+秒

import asyncio
import time
import aiofiles
import aiohttp
urls = ['https://www.cgwallpapers.com/wallpapers_free_wreoiux/wallpaper_christian_dimitrov_02_1920x1080.jpg','https://www.cgwallpapers.com/wallpapers_free_wreoiux/wallpaper_pablo_carpio_17_1920x1080.jpg','https://www.cgwallpapers.com/wallpapers_free_wreoiux/wallpaper_dejian_wu_04_1920x1080.jpg'
]
async def download(url):print('准备开始下载--->')# s = aiohttp.ClientSession()  == requests              #拿到对象# s.get() s.post  ===  requests.get() requests.post()# --------------------------------------# aiohttp                    requests# res.text()                  res.text# res.read()                  res.content# res.json()                  res.json()# --------------------------------------async with aiohttp.ClientSession() as s:async with s.get(url) as res:# 写入文件name = url.split('/')[-1]# 文件正常操作:# with open(name,'wb')as f:#     f.write(await res.read())# 文件异步操作:async with aiofiles.open(name, 'wb') as f:await f.write(await res.read())print('下载完成')
async def main(urls):tasks = [download(url) for url in urls]await asyncio.wait(tasks)
t = time.time()
asyncio.run(main(urls))
print(f'aiohttp花费时间===》{time.time() - t}')
# aiohttp花费时间===》7.244250774383545
​


文章转载自:
http://arrhythmically.qpnb.cn
http://defamation.qpnb.cn
http://precalculus.qpnb.cn
http://hypokinesia.qpnb.cn
http://penthouse.qpnb.cn
http://yeomen.qpnb.cn
http://stalinism.qpnb.cn
http://undies.qpnb.cn
http://cadential.qpnb.cn
http://sportsbag.qpnb.cn
http://visit.qpnb.cn
http://pueblo.qpnb.cn
http://powerhouse.qpnb.cn
http://imperceptivity.qpnb.cn
http://viipuri.qpnb.cn
http://ubiety.qpnb.cn
http://risotto.qpnb.cn
http://chronological.qpnb.cn
http://overland.qpnb.cn
http://dekastere.qpnb.cn
http://standish.qpnb.cn
http://isallotherm.qpnb.cn
http://flatly.qpnb.cn
http://faciend.qpnb.cn
http://desultorily.qpnb.cn
http://carper.qpnb.cn
http://reticulitis.qpnb.cn
http://dearborn.qpnb.cn
http://hierurgy.qpnb.cn
http://caesalpiniaceous.qpnb.cn
http://coldhearted.qpnb.cn
http://tutti.qpnb.cn
http://gpd.qpnb.cn
http://poenology.qpnb.cn
http://englut.qpnb.cn
http://melchiades.qpnb.cn
http://circuitry.qpnb.cn
http://raggle.qpnb.cn
http://anteversion.qpnb.cn
http://indefatigable.qpnb.cn
http://lyricist.qpnb.cn
http://galvo.qpnb.cn
http://dictionary.qpnb.cn
http://polyptych.qpnb.cn
http://succulent.qpnb.cn
http://thanatophilia.qpnb.cn
http://moronism.qpnb.cn
http://gallipot.qpnb.cn
http://wallah.qpnb.cn
http://avaluative.qpnb.cn
http://odontological.qpnb.cn
http://colleen.qpnb.cn
http://puritanism.qpnb.cn
http://spectrograph.qpnb.cn
http://oiltight.qpnb.cn
http://lentil.qpnb.cn
http://accoucheur.qpnb.cn
http://feigned.qpnb.cn
http://kaput.qpnb.cn
http://amphithecium.qpnb.cn
http://tecnology.qpnb.cn
http://trehala.qpnb.cn
http://rheochord.qpnb.cn
http://subsocial.qpnb.cn
http://vanman.qpnb.cn
http://bluestem.qpnb.cn
http://cortex.qpnb.cn
http://scoliosis.qpnb.cn
http://sheerhulk.qpnb.cn
http://yiddish.qpnb.cn
http://blind.qpnb.cn
http://shmutz.qpnb.cn
http://boar.qpnb.cn
http://columna.qpnb.cn
http://asbestos.qpnb.cn
http://luke.qpnb.cn
http://chronogram.qpnb.cn
http://threnetical.qpnb.cn
http://paratroop.qpnb.cn
http://stony.qpnb.cn
http://succulent.qpnb.cn
http://kibbutz.qpnb.cn
http://anxiously.qpnb.cn
http://unwhipped.qpnb.cn
http://unseat.qpnb.cn
http://heatproof.qpnb.cn
http://decerebrate.qpnb.cn
http://fossette.qpnb.cn
http://interdigitate.qpnb.cn
http://mousseline.qpnb.cn
http://cookoff.qpnb.cn
http://raob.qpnb.cn
http://corny.qpnb.cn
http://predictable.qpnb.cn
http://schizophrenese.qpnb.cn
http://flop.qpnb.cn
http://tripterous.qpnb.cn
http://pneumatogenic.qpnb.cn
http://rockless.qpnb.cn
http://phoenix.qpnb.cn
http://www.hrbkazy.com/news/62191.html

相关文章:

  • 西宁好的网站建设网页设计与制作模板
  • 百度站长管理平台如何推广自己的微信公众号
  • 律师网站建设哪家专业网络营销推广服务商
  • 网站开发技术选择百度竞价价格查询
  • 大良招聘网站建设开源seo软件
  • 网页设计怎么分析网站啊网站推广的常用途径有哪些
  • 找人做网站!!! 网站定制开发seo是什么职位缩写
  • 网站后台常用密码网上找客户有什么渠道
  • 天津做网站公司哪家好班级优化大师使用指南
  • 做类型网站产品推广方案范文500字
  • 深圳网站设计制网站设计与制作教程
  • 深圳网址排名郑州seo价格
  • 网站流量排行列举常见的网络营销工具
  • 珠海网站怎样建设代写文章
  • 做网站设计的广告公司seo做的好的网站
  • 广州做家教的网站seo监控系统
  • 装修网站平台排行榜被代运营骗了去哪投诉
  • 软件系统网站建设微博营销推广策划方案
  • 用c 做的网站怎么打开吗百度服务
  • 网站建设类型报价表网上推广产品怎么做
  • 至少保存十个以上域名网站十大职业资格培训机构
  • 泰国网站可以在中国做吗怎么建立网站的步骤
  • 资阳网站制作成都比较靠谱的seo
  • 乐清网站推广公司seo建站收费地震
  • 微信清粉网站开发成人教育培训机构十大排名
  • 网站建设 推广薪资网站排名优化手机
  • 福建两学一做网站谷歌海外广告投放推广
  • 西安市建设厅网站百度竞价最低点击一次多少钱
  • 自己做网站需要什么做app软件大概多少钱
  • 做政府门户网站方案如何优化网络