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

一般找素材都是做哪几个网站呢网站建设设计报告

一般找素材都是做哪几个网站呢,网站建设设计报告,网站什么做才会更吸引客户,最新新闻事件今天国内大事2022目录 1、ants介绍 2、使用方式汇总 3、各种使用方式详解 3.1 默认池 3.2 普通模式 3.3 带参函数 3.4 多池多协程 4、总结 1、ants介绍 众所周知#xff0c;goroutine相比于线程来说#xff0c;更加轻量、资源占用更少、无线程上下文切换等优势#xff0c;但是也不能…目录 1、ants介绍 2、使用方式汇总 3、各种使用方式详解 3.1 默认池 3.2 普通模式 3.3 带参函数 3.4 多池多协程 4、总结 1、ants介绍 众所周知goroutine相比于线程来说更加轻量、资源占用更少、无线程上下文切换等优势但是也不能无节制的创建使用如果系统中开启的goroutine过多而没有及时回收也会造成系统内存资源耗尽。 ants是一款高性能的协程管理池实现了协程的创建、缓存、复用、刷新、停止能功能同时允许开发者设置线程池中worker的数量、线程池本身的个数以及workder中的任务从而实现更加高效的运行效果。 githubGitHub - 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 dont 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 GOMAXPROCS1.// 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 GOMAXPROCS1,// 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 (fmtsynctimegithub.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 (fmtsynctimegithub.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 (fmtsynctimegithub.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)) }运行结果 liupengliupengdeMacBook-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 (fmtsynctimegithub.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://www.dnsts.com.cn/news/91264.html

相关文章:

  • 重庆专业做淘宝网站苏州工业园区公共资源交易中心
  • 便宜手机网站建设设计成功一个电子商务网站
  • 有没类似建设通的免费网站微信快速赚100块
  • 开封网站建设培训学校外贸业务员面试常见问题
  • 爱站seo查询软件内蒙古乌海建设局网站
  • 谁做网站做公司展示网站
  • 如何用网站赚钱wordpress 文章 页码
  • 永久免费wap建站网站建设互联
  • 贵州省建设监理协会官方网站直播软件有哪些
  • 做设计时可以参考的网站六安搜索引擎优化方法
  • 做中学数学教案有哪些好的网站网站建设的规划
  • 婴儿网站模板焦作网站建设哪家正规
  • 做网站的人能看到浏览的人的信息吗招聘做牙技工的网站
  • 网站开发哪家好中文响应式网站模板
  • 手机如何网站广源建设集团网站
  • 公司的网站建设哪家比较好南宁住房和城乡建设局网站
  • 网站备案登录密码找回网站logo如何修改
  • 四川省建设厅职称网站网站规划的任务
  • 更换dns能上国外网站吗商城网站开发 多少钱
  • 外贸购物网站制作文学网站建设平台
  • 网站搜索功能怎么做热搜榜上能否吃自热火锅
  • 保山网站建设报价网站推广含义
  • 名师工作室建设网站做网站插背景图片如何变大
  • 网站设计制作的介绍中建集团招聘信息官网
  • 深圳集团网站开发网站开发公司电话美工素材网站
  • 德州企业认证网站建设小程序问答库
  • 合肥网站排名优化公司市场营销互联网营销
  • 石家庄红酒公司 网站建设滑县网站建设哪家好
  • 班级建设怎样建立班级网站wordpress 评论 html
  • seo做的不好的网站做拍卖的网站有哪些