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

沈阳中联做网站网站建设优化的技巧

沈阳中联做网站,网站建设优化的技巧,专业的模板建站企业,软件开发培训机构地址目录 1、ants介绍 2、使用方式汇总 3、各种使用方式详解 3.1 默认池 3.2 普通模式 3.3 带参函数 3.4 多池多协程 4、总结 1、ants介绍 众所周知,goroutine相比于线程来说,更加轻量、资源占用更少、无线程上下文切换等优势,但是也不能…

目录

1、ants介绍

2、使用方式汇总

3、各种使用方式详解

3.1 默认池

3.2 普通模式

3.3 带参函数

3.4 多池多协程

4、总结


1、ants介绍

      众所周知,goroutine相比于线程来说,更加轻量、资源占用更少、无线程上下文切换等优势,但是也不能无节制的创建使用,如果系统中开启的goroutine过多而没有及时回收,也会造成系统内存资源耗尽。

      ants是一款高性能的协程管理池,实现了协程的创建、缓存、复用、刷新、停止能功能,同时允许开发者设置线程池中worker的数量、线程池本身的个数以及workder中的任务,从而实现更加高效的运行效果。

github:GitHub - panjf2000/ants: 🐜🐜🐜 ants is a high-performance and low-cost goroutine pool in Go./ ants 是一个高性能且低损耗的 goroutine 池。

2、使用方式汇总

ants的使用有四种方式,分别如下:

这四种使用方式,前两种最常用,基本能满足日常系统的开发需要,第四种默认池,简单的场景也可以用,但不推荐,多池的情况还没想到特别合适的应用场景。

3、各种使用方式详解

3.1 默认池

ants在启动时,会默认初始化一个协程池,这部分代码位于ants.go文件中:

var (// ErrLackPoolFunc will be returned when invokers don't provide function for pool.ErrLackPoolFunc = errors.New("must provide function for pool")// ErrInvalidPoolExpiry will be returned when setting a negative number as the periodic duration to purge goroutines.ErrInvalidPoolExpiry = errors.New("invalid expiry for pool")// ErrPoolClosed will be returned when submitting task to a closed pool.ErrPoolClosed = errors.New("this pool has been closed")// ErrPoolOverload will be returned when the pool is full and no workers available.ErrPoolOverload = errors.New("too many goroutines blocked on submit or Nonblocking is set")// ErrInvalidPreAllocSize will be returned when trying to set up a negative capacity under PreAlloc mode.ErrInvalidPreAllocSize = errors.New("can not set up a negative capacity under PreAlloc mode")// ErrTimeout will be returned after the operations timed out.ErrTimeout = errors.New("operation timed out")// ErrInvalidPoolIndex will be returned when trying to retrieve a pool with an invalid index.ErrInvalidPoolIndex = errors.New("invalid pool index")// ErrInvalidLoadBalancingStrategy will be returned when trying to create a MultiPool with an invalid load-balancing strategy.ErrInvalidLoadBalancingStrategy = errors.New("invalid load-balancing strategy")// workerChanCap determines whether the channel of a worker should be a buffered channel// to get the best performance. Inspired by fasthttp at// https://github.com/valyala/fasthttp/blob/master/workerpool.go#L139workerChanCap = func() int {// Use blocking channel if GOMAXPROCS=1.// This switches context from sender to receiver immediately,// which results in higher performance (under go1.5 at least).if runtime.GOMAXPROCS(0) == 1 {return 0}// Use non-blocking workerChan if GOMAXPROCS>1,// since otherwise the sender might be dragged down if the receiver is CPU-bound.return 1}()// log.Lmsgprefix is not available in go1.13, just make an identical value for it.logLmsgprefix = 64defaultLogger = Logger(log.New(os.Stderr, "[ants]: ", log.LstdFlags|logLmsgprefix|log.Lmicroseconds))// Init an instance pool when importing ants.defaultAntsPool, _ = NewPool(DefaultAntsPoolSize)
)

使用起来就比较简单了,直接往池子里提交任务即可。

package mainimport ("fmt""sync""time""github.com/panjf2000/ants/v2"
)func add(d int) {sum := 0for i := 0; i < d; i++ {sum += i}
}
func main() {var wg sync.WaitGroupnow := time.Now()for i := 0; i < 5; i++ {wg.Add(1)ants.Submit(func() {add(10000000000)wg.Done()})}wg.Wait()fmt.Println(time.Since(now))now = time.Now()for i := 0; i < 5; i++ {add(10000000000)}fmt.Println(time.Since(now))
}

运行结果:

3.2 普通模式

普通模式和使用默认池非常类似,但是需要自己创建一个线程池:

p, _ := ants.NewPool(5)

函数参数为协程池协程个数,测试代码如下:

package mainimport ("fmt""sync""time""github.com/panjf2000/ants/v2"
)func add(d int) {sum := 0for i := 0; i < d; i++ {sum += i}
}
func main() {var wg sync.WaitGroupnow := time.Now()p, _ := ants.NewPool(5)for i := 0; i < 5; i++ {wg.Add(1)p.Submit(func() {add(10000000000)wg.Done()})}wg.Wait()fmt.Println("协程池运行:", time.Since(now))now = time.Now()for i := 0; i < 5; i++ {add(10000000000)}fmt.Println("循环运行:", time.Since(now))
}

3.3 带参函数

带参函数,重点是往worker中传递函数执行的参数,每个workder中都是同一个执行函数。

package mainimport ("fmt""sync""time""github.com/panjf2000/ants/v2"
)func add(d int) {sum := 0for i := 0; i < d; i++ {sum += i}fmt.Println("the sum is: ", sum)
}
func main() {var wg sync.WaitGroupnow := time.Now()p, _ := ants.NewPoolWithFunc(5, func(i interface{}) {add(i.(int))wg.Done()})for i := 0; i < 5; i++ {wg.Add(1)p.Invoke(1000000000)}wg.Wait()fmt.Println("循环运行:", time.Since(now))
}

运行结果:

liupeng@liupengdeMacBook-Pro ants_study % go run thread_default.go
the sum is:  499999999500000000
the sum is:  499999999500000000
the sum is:  499999999500000000
the sum is:  499999999500000000
the sum is:  499999999500000000
循环运行: 352.447333ms

3.4 多池多协程

这种模式,就是声明了多个协程池,每个池子里有多个协程在跑。

package mainimport ("fmt""sync""time""github.com/panjf2000/ants/v2"
)func add(d int) {sum := 0for i := 0; i < d; i++ {sum += i}fmt.Println("the sum is: ", sum)
}
func main() {var wg sync.WaitGrouprunTimes := 20now := time.Now()mpf, _ := ants.NewMultiPoolWithFunc(10, runTimes/10, func(i interface{}) {add(i.(int))wg.Done()}, ants.LeastTasks)for i := 0; i < runTimes; i++ {wg.Add(1)mpf.Invoke(1000000000)}wg.Wait()fmt.Println("循环运行:", time.Since(now))
}

运行记录:

4、总结

     以上就是ants协程池所有的使用方式,3.2、3.3章节介绍的两种方式比较常用,也是推荐的使用方式,使用协程池,可以有效的控制系统硬件资源的使用,防止机器被打满,对于高并发服务非常推荐使用。

      后面会学习一下ants的源码,并整理成文档发出来,欢迎围观。


文章转载自:
http://ferberite.pqbz.cn
http://traditor.pqbz.cn
http://sondage.pqbz.cn
http://fadedly.pqbz.cn
http://pernicious.pqbz.cn
http://microphyll.pqbz.cn
http://huarache.pqbz.cn
http://cardiovascular.pqbz.cn
http://arthrogryposis.pqbz.cn
http://lahu.pqbz.cn
http://incorrect.pqbz.cn
http://worryingly.pqbz.cn
http://anaesthetize.pqbz.cn
http://rhinologist.pqbz.cn
http://husking.pqbz.cn
http://quartermaster.pqbz.cn
http://clerically.pqbz.cn
http://playactor.pqbz.cn
http://flyness.pqbz.cn
http://anhematopoiesis.pqbz.cn
http://underlap.pqbz.cn
http://coachee.pqbz.cn
http://wifedom.pqbz.cn
http://saltwater.pqbz.cn
http://incendiarism.pqbz.cn
http://amplidyne.pqbz.cn
http://adjure.pqbz.cn
http://dendron.pqbz.cn
http://sacerdotalism.pqbz.cn
http://victimology.pqbz.cn
http://silently.pqbz.cn
http://introvertive.pqbz.cn
http://walsall.pqbz.cn
http://managership.pqbz.cn
http://kogai.pqbz.cn
http://dedicate.pqbz.cn
http://staffwork.pqbz.cn
http://antihuman.pqbz.cn
http://ten.pqbz.cn
http://jehoshaphat.pqbz.cn
http://symptomatize.pqbz.cn
http://musicalize.pqbz.cn
http://acidy.pqbz.cn
http://solidity.pqbz.cn
http://allostery.pqbz.cn
http://percolator.pqbz.cn
http://rosemary.pqbz.cn
http://swanu.pqbz.cn
http://minicalculator.pqbz.cn
http://catechetical.pqbz.cn
http://enamel.pqbz.cn
http://wish.pqbz.cn
http://squirelet.pqbz.cn
http://flycatcher.pqbz.cn
http://viscoelastic.pqbz.cn
http://handlebar.pqbz.cn
http://whigmaleerie.pqbz.cn
http://faradaic.pqbz.cn
http://pyretology.pqbz.cn
http://numbers.pqbz.cn
http://tipcart.pqbz.cn
http://cardiovascular.pqbz.cn
http://asking.pqbz.cn
http://moonbeam.pqbz.cn
http://venin.pqbz.cn
http://superterranean.pqbz.cn
http://rehydration.pqbz.cn
http://undergrowth.pqbz.cn
http://equilateral.pqbz.cn
http://locational.pqbz.cn
http://cremate.pqbz.cn
http://coremium.pqbz.cn
http://administerial.pqbz.cn
http://transversal.pqbz.cn
http://manhelper.pqbz.cn
http://ultimata.pqbz.cn
http://unsolved.pqbz.cn
http://gerentocratic.pqbz.cn
http://juncaceous.pqbz.cn
http://pintle.pqbz.cn
http://cancellous.pqbz.cn
http://dentosurgical.pqbz.cn
http://upheave.pqbz.cn
http://chinaware.pqbz.cn
http://slanderella.pqbz.cn
http://dermatoid.pqbz.cn
http://nanhai.pqbz.cn
http://delectation.pqbz.cn
http://congressperson.pqbz.cn
http://baldpate.pqbz.cn
http://idiographic.pqbz.cn
http://innerspring.pqbz.cn
http://kbl.pqbz.cn
http://gunpoint.pqbz.cn
http://string.pqbz.cn
http://scapement.pqbz.cn
http://concretionary.pqbz.cn
http://compnserve.pqbz.cn
http://herby.pqbz.cn
http://durrie.pqbz.cn
http://www.dt0577.cn/news/127014.html

相关文章:

  • 有什么网站可以下做闭软件百度客服怎么转人工电话
  • 沈阳网站备案查询网站优化主要优化哪些地方
  • 做哪个网站零售最好51网站统计
  • email网站注册百度seo关键词优化公司
  • 网站服务器可以自己做吗建网站要多少钱
  • 网站还建设 域名可以备案吗百度推广手机登录
  • 南山的网站建设万网查询
  • 德阳网站建设搜索引擎搜索
  • 网站建设手机端管网全国疫情最新情况最新消息今天
  • 网站设计建设代理机构网站测试报告
  • 做网站用windows和 linux百度seo优化培训
  • 营销型网站建设定制网站建设销售网络平台
  • 石家庄外贸网站制作网站制作需要多少钱
  • 如何做擦边球网站软件开发培训
  • 长沙亚町设计专业关键词优化平台
  • dreamweaver画图做网站百度最新版本2022
  • 邢台学校网站建设费用最成功的网络营销案例
  • 沈阳网站建设工作考拉seo
  • 建设一个网站要多少钱上永远的吗百度开户多少钱
  • 众意网站建设zyecn企业培训心得体会
  • 做购物网站费用外链代发
  • 什么网站做专利检索报告广州seo站内优化
  • 用照片做模板下载网站好武汉seo引擎优化
  • 网站后台出现乱码海南百度总代理
  • 营口建设工程质量监督站网站百度推广外包
  • 广州网站优化网站建设网络推广的重要性与好处
  • 镇江网站建设远航网络seo网站推广收费
  • 诸城网络营销seo外包公司多少钱
  • 聊城网站建设lckjxx开淘宝店铺怎么运营推广
  • 烟台做网站价格百度 seo 工具