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

怎么在百度提交网站大数据平台建站

怎么在百度提交网站,大数据平台建站,wordpress加百度一下,网站开发学什么语音文章目录 前言一、单个任务的取消二、 所有任务取消三、Context的出现Context的定义Context使用 总结 前言 在实际的业务种#xff0c;我们可能会有这么一种场景#xff1a;需要我们主动的通知某一个goroutine结束。比如我们开启一个后台goroutine一直做事情#xff0c;比如… 文章目录 前言一、单个任务的取消二、 所有任务取消三、Context的出现Context的定义Context使用 总结 前言 在实际的业务种我们可能会有这么一种场景需要我们主动的通知某一个goroutine结束。比如我们开启一个后台goroutine一直做事情比如监控现在不需要了就需要通知这个监控goroutine结束不然它会一直跑就泄漏了。 我们都知道一个goroutine启动后我们是无法控制他的大部分情况是等待它自己结束那么如果这个goroutine是一个不会自己结束的后台goroutine呢比如监控等会一直运行的。 下面我们分别介绍 chanselect 方式 和 Context方式任务的取消的方法。 一、单个任务的取消 下面的代码代表的场景是一个监控的任务一直在执行中通过一个无缓存信道在接受到bool的值的时候协程内部通过多路复用进入监控停止的逻辑退出任务。 package mainimport (fmttime )func cancel_1(stop chan bool) {stop - true }func monitor(name string, stop chan bool) {for {select {case -stop:fmt.Printf(%v 监控退出停止了...\n, name)returndefault:fmt.Printf(%v, goroutine监控中... \n, name)time.Sleep(2 * time.Second)}} }func main() {stop : make(chan bool)go monitor(1号, stop)time.Sleep(10 * time.Second)fmt.Println(可以了通知监控停止)cancel_1(stop)//为了检测监控过是否停止如果没有监控输出就表示停止了time.Sleep(5 * time.Second) } 代码执行结果如下 [rootwork day01]# go run context.go 1号, goroutine监控中... 1号, goroutine监控中... 1号, goroutine监控中... 1号, goroutine监控中... 1号, goroutine监控中... 可以了通知监控停止 1号 监控退出停止了...现在稍微改造下上面的代码我们再增加几个goroutine。2号3号 。。。 func main() {stop : make(chan bool)go monitor(1号, stop)go monitor(2号, stop)go monitor(3号, stop)......... }再次执行查看结果 [rootwork day01]# go run context.go 3号, goroutine监控中... 1号, goroutine监控中... 2号, goroutine监控中... 3号, goroutine监控中... 1号, goroutine监控中... 2号, goroutine监控中... 2号, goroutine监控中... 1号, goroutine监控中... 3号, goroutine监控中... 1号, goroutine监控中... 3号, goroutine监控中... 2号, goroutine监控中... 1号, goroutine监控中... 2号, goroutine监控中... 3号, goroutine监控中... 可以了通知监控停止 1号 监控退出停止了... 3号, goroutine监控中... 2号, goroutine监控中... 2号, goroutine监控中... 3号, goroutine监控中... 3号, goroutine监控中... 2号, goroutine监控中... 发现这种方式只停了其中的一个任务。其他两个任务还在执行。 如何保证所有的任务都停止呢我们看下一节 二、 所有任务取消 我们改造代码如下 新增一个cancel_2的方法再main函数调用cancel_2。 package mainimport (fmttime )func cancel_1(stop chan bool) {stop - true }func cancel_2(stop chan bool) {close(stop) }func monitor(name string, stop chan bool) {for {select {case -stop:fmt.Printf(%v 监控退出停止了...\n, name)returndefault:fmt.Printf(%v, goroutine监控中... \n, name)time.Sleep(2 * time.Second)}} }func main() {stop : make(chan bool)go monitor(1号, stop)go monitor(2号, stop)go monitor(3号, stop)time.Sleep(10 * time.Second)fmt.Println(可以了通知监控停止)cancel_2(stop)//为了检测监控过是否停止如果没有监控输出就表示停止了time.Sleep(5 * time.Second)} 执行结果如下 [rootwork day01]# go run context.go 3号, goroutine监控中... 1号, goroutine监控中... 2号, goroutine监控中... 2号, goroutine监控中... 1号, goroutine监控中... 3号, goroutine监控中... 1号, goroutine监控中... 2号, goroutine监控中... 3号, goroutine监控中... 3号, goroutine监控中... 1号, goroutine监控中... 2号, goroutine监控中... 1号, goroutine监控中... 2号, goroutine监控中... 3号, goroutine监控中... 可以了通知监控停止 3号 监控退出停止了... 2号 监控退出停止了... 1号 监控退出停止了...可以看到三个goroutine都停止了这里我们用到了关闭通道的广播机制所有通道接收者都会收到关闭的消息。 上面的例子说明当我们定义一个无缓冲通道时如果要对所有的 goroutine 进行关闭可以使用 close 关闭通道然后在所有的 goroutine 里不断检查通道是否关闭(前提你得约定好该通道你只会进行 close 而不会发送其他数据否则发送一次数据就会关闭一个goroutine这样会不符合咱们的预期所以最好你对这个通道再做一层封装做个限制)来决定是否结束 goroutine。 三、Context的出现 前两节chanselect的方式是比较优雅的结束一个goroutine的方式不过这种方式也有局限性如果有很多goroutine都需要控制结束怎么办呢如果这些goroutine又衍生了其他更多的goroutine怎么办呢如果一层层的无穷尽的goroutine呢如下图当取消Handl(Req1)这个节点的任务希望取消与他关联的子任务的时候。我们再通过上面方式是不是就需要修改很多呢。所以Context的真正用途出现了。 Context的定义 Context也叫上下文它的接口定义如下 type Context interface {Deadline() (deadline time.Time, ok bool)Done() -chan struct{}Err() errorValue(key interface{}) interface{} }Deadline返回的第一个值是 截止时间到了这个时间点Context 会自动触发 Cancel 动作。返回的第二个值是 一个布尔值true 表示设置了截止时间false 表示没有设置截止时间如果没有设置截止时间就要手动调用 cancel 函数取消Context。Done返回一个只读的通道只有在被cancel后才会返回类型为 struct{}。当这个通道可读时意味着parent context已经发起了取消请求根据这个信号开发者就可以做一些清理动作退出goroutine。Err返回 context 被 cancel 的原因。Value返回被绑定到 Context 的值是一个键值对所以要通过一个Key才可以获取对应的值这个值一般是线程安全的。 Context使用 使用Context完成 package mainimport (contextfmttime )func monitor(name string, ctx context.Context) {for {select {case -ctx.Done():fmt.Printf(%v 监控退出停止了...\n, name)returndefault:fmt.Printf(%v, goroutine监控中... \n, name)time.Sleep(2 * time.Second)}} }func main() {ctx, cancel : context.WithCancel(context.Background())go monitor(1号, ctx)go monitor(2号, ctx)go monitor(3号, ctx)time.Sleep(10 * time.Second)fmt.Println(可以了通知监控停止)cancel()//为了检测监控过是否停止如果没有监控输出就表示停止了time.Sleep(5 * time.Second) }执行结果 [rootwork day01]# go run context2.go 1号, goroutine监控中... 3号, goroutine监控中... 2号, goroutine监控中... 1号, goroutine监控中... 3号, goroutine监控中... 2号, goroutine监控中... 2号, goroutine监控中... 3号, goroutine监控中... 1号, goroutine监控中... 1号, goroutine监控中... 2号, goroutine监控中... 3号, goroutine监控中... 2号, goroutine监控中... 1号, goroutine监控中... 3号, goroutine监控中... 可以了通知监控停止 3号 监控退出停止了... 1号 监控退出停止了... 2号 监控退出停止了...总结 任务的取消一般可以采用chan select方式但是任务依赖比较复杂的情况推荐使用Context通常 Context 都是做为函数的第一个参数进行传递规范性做法并且变量名建议统一叫 ctxContext 是线程安全的可以放心地在多个 goroutine 中使用。当你把 Context 传递给多个 goroutine 使用时只要执行一次 cancel 操作所有的 goroutine 就可以收到 取消的信号当一个 Context 被 cancel 时继承自该 Context 的所有 子 Context 都会被 cancel。
http://www.pierceye.com/news/157535/

相关文章:

  • 网站建设项目报告总结报告seo关于网站搜索排名关键词的标准评定
  • 东莞电商网站建设wordpress注册验证邮箱
  • 网站建设名中国建设劳动学会是假网站吗
  • 一个优秀的个人网站百度极速版免费下载安装
  • 咋做211校徽加网站wordpress免费教程视频教程
  • 网站建设制作网络营销公司蛋糕店网站模板
  • a站网址东莞市网络seo推广价格
  • 莱州市双语网站seo白帽优化
  • 不忘初心网站建设深圳公租房官网
  • 网站点击率原因深圳做自适应网站制作
  • 上海个人建站小程序注册完成后如何制作
  • 微网站开发平台 开源大庆做网站公司
  • 长沙市住房和城乡建设局网站wordpress付费可见插件
  • 建设个人网站的参考网站及文献辽宁建设工程造价管理网站
  • 如何做360网站的排名新品发布会策划方案ppt
  • 网站后台登陆破解哪里有网站模板下载
  • 网站制作器软件下载建站备案
  • 网页模板下载网站站长素材音效网
  • 青岛网站建设要多少钱关键词优化是怎样收费的
  • 网站国际联网备案WordPress文章分页伪静态
  • 电子商务网站开发的任务书东莞seo关键词搜索关键词
  • 宁乡网站建设在哪小天才电话手表网站
  • 中文响应式网站搜搜网站提交
  • 华为官方网站手机商城首页大淘客网站商品做淘口令
  • 建站公司网站的关键词怎么设置
  • 上海二手房网站制作游戏需要什么技术
  • 湖州市城市建设档案馆网站电子商务网站怎么建
  • 网站超级外链做企业网站大约多少钱
  • 中国网站建设市场分析桂林公司网站搭建
  • 阿里云 企业网站选哪种洞窝app是谁开发的