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

吴川网站建设公司wordpress页面能用js吗

吴川网站建设公司,wordpress页面能用js吗,手机建设银行官方网站,没有域名 有公网ip 建网站可以文章目录 一、简介二、Context 的基本概念1. context 包常用函数 三、Context 的基本用法1. WithCancel#xff1a;取消任务的上下文 四、超时控制#xff1a;WithTimeout 和 WithDeadline1. 使用 WithTimeout 控制任务超时2. 使用 WithDeadline 设定截止时间 五、传递上下文… 文章目录 一、简介二、Context 的基本概念1. context 包常用函数 三、Context 的基本用法1. WithCancel取消任务的上下文 四、超时控制WithTimeout 和 WithDeadline1. 使用 WithTimeout 控制任务超时2. 使用 WithDeadline 设定截止时间 五、传递上下文中的数据WithValue六、Context 的应用场景七、完整示例多任务协作控制八、小结 一、简介 在并发编程中任务管理和资源控制是非常重要的而 Golang 的 context 包 为我们提供了一种优雅的方式来传递取消信号和超时控制。Context 用于在多个 Goroutine 之间传递上下文信息避免 Goroutine 无法按需停止而导致资源浪费。 本篇博客将详细介绍 context 包的用法并通过实例讲解如何在超时、取消任务和多 Goroutine 协作场景中使用它。 二、Context 的基本概念 Context 是一种携带取消信号、截止时间超时和元数据的上下文对象主要用于父 Goroutine 与子 Goroutine 的协作。它通过层级化的结构来管理多个并发任务。 1. context 包常用函数 context.Background()创建根上下文通常用于程序入口。context.TODO()占位符上下文表示未来会替换为实际上下文。context.WithCancel(parent Context)创建带取消功能的子上下文。context.WithTimeout(parent Context, timeout time.Duration)创建带超时功能的子上下文。context.WithDeadline(parent Context, deadline time.Time)基于指定的截止时间创建上下文。context.WithValue(parent Context, key, value interface{})传递携带额外数据的上下文。 三、Context 的基本用法 1. WithCancel取消任务的上下文 示例使用 WithCancel 取消 Goroutine package mainimport (contextfmttime )func worker(ctx context.Context, id int) {for {select {case -ctx.Done(): // 接收取消信号fmt.Printf(Worker %d stopped\n, id)returndefault:fmt.Printf(Worker %d is working...\n, id)time.Sleep(time.Second)}} }func main() {ctx, cancel : context.WithCancel(context.Background()) // 创建可取消的上下文for i : 1; i 3; i {go worker(ctx, i)}time.Sleep(3 * time.Second) // 模拟主 Goroutine 的其他工作fmt.Println(Cancelling all workers...)cancel() // 发送取消信号time.Sleep(1 * time.Second) // 等待所有 Goroutine 退出fmt.Println(All workers stopped.) }输出 Worker 1 is working... Worker 2 is working... Worker 3 is working... ... Cancelling all workers... Worker 1 stopped Worker 2 stopped Worker 3 stopped All workers stopped.解析 context.WithCancel 创建的上下文可以通过调用 cancel() 发送取消信号从而优雅地停止所有子 Goroutine。 四、超时控制WithTimeout 和 WithDeadline 1. 使用 WithTimeout 控制任务超时 示例在 2 秒内完成任务否则超时退出 package mainimport (contextfmttime )func worker(ctx context.Context) {select {case -time.After(3 * time.Second): // 模拟长时间任务fmt.Println(Task completed)case -ctx.Done(): // 接收超时信号fmt.Println(Task timed out)} }func main() {ctx, cancel : context.WithTimeout(context.Background(), 2*time.Second) // 设置 2 秒超时defer cancel() // 确保资源释放go worker(ctx)time.Sleep(4 * time.Second) // 等待任务完成或超时 }输出 Task timed out解析 通过 context.WithTimeout 创建的上下文2 秒内未完成任务时自动发送取消信号。超时上下文可避免 Goroutine 无限期运行帮助更好地管理资源。 2. 使用 WithDeadline 设定截止时间 WithDeadline 和 WithTimeout 类似只是使用具体的时间点来控制超时。 五、传递上下文中的数据WithValue 有时我们需要在多个 Goroutine 之间传递一些元数据。WithValue 允许我们将键值对存入上下文并在子 Goroutine 中访问。 示例传递用户信息 package mainimport (contextfmttime )func greetUser(ctx context.Context) {if user, ok : ctx.Value(user).(string); ok {fmt.Printf(Hello, %s!\n, user)} else {fmt.Println(No user found.)} }func main() {ctx : context.WithValue(context.Background(), user, Alice) // 在上下文中存入用户信息go greetUser(ctx)time.Sleep(1 * time.Second) // 确保 Goroutine 执行完毕 }输出 Hello, Alice!解析 WithValue 允许我们为上下文设置键值对便于在多 Goroutine 间传递数据。 注意 不建议用 WithValue 传递重要的控制信息例如取消信号或超时。 六、Context 的应用场景 API 请求的超时控制确保 HTTP 请求不会无限期等待。任务取消当用户主动取消某个操作时通知相关的 Goroutine 停止工作。传递元数据例如在服务链路中传递用户身份、请求 ID 等信息。 七、完整示例多任务协作控制 示例启动多个任务随时可取消所有任务 package mainimport (contextfmtsynctime )func worker(ctx context.Context, id int, wg *sync.WaitGroup) {defer wg.Done()for {select {case -ctx.Done():fmt.Printf(Worker %d stopped\n, id)returndefault:fmt.Printf(Worker %d is processing...\n, id)time.Sleep(500 * time.Millisecond)}} }func main() {var wg sync.WaitGroupctx, cancel : context.WithCancel(context.Background()) // 创建上下文for i : 1; i 3; i {wg.Add(1)go worker(ctx, i, wg)}time.Sleep(2 * time.Second)fmt.Println(Cancelling all workers...)cancel() // 取消所有任务wg.Wait() // 等待所有任务完成fmt.Println(All workers stopped.) }输出 Worker 1 is processing... Worker 2 is processing... Worker 3 is processing... ... Cancelling all workers... Worker 1 stopped Worker 2 stopped Worker 3 stopped All workers stopped.八、小结 Context 用于并发控制在 Goroutine 中传递取消信号、超时信号或携带元数据。超时控制与资源管理使用 WithTimeout 和 WithCancel 及时终止任务避免资源浪费。多 Goroutine 协作通过 Context 实现多个 Goroutine 之间的优雅通信。
http://www.hkea.cn/news/14491827/

相关文章:

  • 摇钱树手机论坛网站网站建设哪个部门管理
  • 设置网站404做网站服务器在哪买
  • 西安最好的网站建设公司wordpress 别名
  • 如何制作课程网站模板下载企管宝官网
  • 网站首页轮播图片素材天元建设集团有限公司刘士龙
  • 手机制作最简单钓鱼网站设计说明书
  • 微商城手机网站模板企业网站开发创意
  • 港海建设网站兰州网站建设加q.479185700
  • 网站公司怎么做的h5商城网站开发
  • 网站优化 seo和sem网站开发名片
  • 小白网站建设教程谷歌seo优化是什么
  • 网站开发毕业答辩个人网站推广 公司
  • 重庆定制网站建设地址html访问wordpress
  • 网站如何定位网站建设需要的设备
  • 网站建设前期新浦网站制作
  • 如何构建自己的网站苏州网站创建
  • 东营网站湛江vi设计公司
  • 现在哪些做进口商品的电商网站海外精品网站建设
  • 企业网站设计与建设北京营销公司比较好的
  • 网站开发前台代码和后台代码wordpress 改变滑页
  • 一站式做网站系统wordpress取消邮箱验证
  • html5怎么做二手网站黄页88网注册
  • 新乡网站建设设计公司哪家好wordpress查看自己网站的ip量
  • 通辽网站开发招聘网页版微信登录入口密码登录
  • 做系统网站赚钱中国作风建设门户网站
  • asp网站怎么运行40个常见的html标签及含义
  • 找制作网站公司河北秦皇岛黄金海岸
  • 汕头网站建设制作公司平台及服务是什么
  • 托里县城乡建设局网站网站建设 案例展示
  • 电子手表网站wordpress打开只显示代码