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

wordpress 图片无法上传成都seo论坛

wordpress 图片无法上传,成都seo论坛,政务大厅网站建设管理制度,澳门118开118186com1. 什么是 Context? Go 1.7 标准库引入 context,中文译作“上下文”,准确说它是 goroutine 的上下文,包含 goroutine 的运行状态、环境、现场等信息。 context 主要用来在 goroutine 之间传递上下文信息,包括&#x…

1. 什么是 Context?

Go 1.7 标准库引入 context,中文译作“上下文”,准确说它是 goroutine 的上下文,包含 goroutine 的运行状态、环境、现场等信息。

context 主要用来在 goroutine 之间传递上下文信息,包括:取消信号、超时时间、截止时间、k-v 等。

Context,也叫上下文,它的接口定义如下

type Context interface {Deadline() (deadline time.Time, ok bool)Done() <-chan struct{}Err() errorValue(key interface{}) interface{}
}

可以看到 Context 接口共有 4 个方法

  • Deadline:返回的第一个值是截止时间,到了这个时间点,Context 会自动触发 Cancel 动作。返回的第二个值是 一个布尔值,true 表示设置了截止时间,false 表示没有设置截止时间,如果没有设置截止时间,就要手动调用 cancel 函数取消 Context。

  • Done:返回一个只读的通道(只有在被cancel后才会返回),类型为 struct{}。当这个通道可读时,意味着parent context已经发起了取消请求,根据这个信号,开发者就可以做一些清理动作,退出goroutine。

  • Err:返回 context 被 cancel 的原因。

  • Value:返回被绑定到 Context 的值,是一个键值对,所以要通过一个Key才可以获取对应的值,这个值一般是线程安全的。

2. 为什么要用Context

  • 用于控制goroutine的结束,但它解决的并不是 能不能 的问题,而是解决 更好用 的问题。

2.1. 当不用Context时,利用channel+select来主动让goroutine停止

示例1


package mainimport ("fmt""time"
)/*
1. 利用channel控制goroutine的停止
*/func main() {stopChan := make(chan bool)go func() {for {select {case <-stopChan:fmt.Println("goroutin1 exit.")returndefault:fmt.Println("goroutin1 sleep 1s, keep going.")time.Sleep(time.Second * 2)}}}()go func() {for {select {case <-stopChan:fmt.Println("goroutin2 exit.")returndefault:fmt.Println("goroutin2 sleep 1s, keep going.")time.Sleep(time.Second * 3)}}}()time.Sleep(10 * time.Second)fmt.Println("10s 时间到了,主进程需要退出了.")// 发送信号让goroute1结束stopChan <- true// 发送信号让goroute2结束stopChan <- truetime.Sleep(5 * time.Second)
}

示例2

package mainimport ("fmt""time"
)/*1. 利用关闭channel的方法,让2个goroutine同时结束
*/func main() {stopChan := make(chan bool)go func() {for {select {case <-stopChan:fmt.Println("goroutin1 exit.")returndefault:fmt.Println("goroutin1 sleep 1s, keep going.")time.Sleep(time.Second * 2)}}}()go func() {for {select {case <-stopChan:fmt.Println("goroutin2 exit.")returndefault:fmt.Println("goroutin2 sleep 1s, keep going.")time.Sleep(time.Second * 3)}}}()time.Sleep(10 * time.Second)fmt.Println("10s 时间到了,主进程需要退出了.")// 利用关闭channel的方法,让2个goroutine同时结束close(stopChan)time.Sleep(5 * time.Second)
}

2.2 使用context来主动让goroutine停止

先ctx, cancel := context.WithCancel(context.Background()) 创建一个ctx实例
再利用cancel()函数执行控制goroutine的停止

package mainimport ("context""fmt""time"
)/*
// 利用context,手动让2个goroutine同时结束[是不是更简单?]
*/
func main() {ctx, cancel := context.WithCancel(context.Background())go func() {for {select {case <-ctx.Done():fmt.Println("goroutin1 exit.")returndefault:fmt.Println("goroutin1 sleep 1s, keep going.")time.Sleep(time.Second * 1)}}}()go func() {for {select {case <-ctx.Done():fmt.Println("goroutin2 exit.")returndefault:fmt.Println("goroutin2 sleep 1s, keep going.")time.Sleep(time.Second * 1)}}}()time.Sleep(10 * time.Second)fmt.Println("10s 时间到了,goroutine需要退出了.")// 利用context的方法,手动让2个goroutine同时结束cancel()time.Sleep(5 * time.Second)
}

2.3 使用context实现goroutine的超时控制

  • 使用场景:让goroutine执行一个任务,如果在指定时间内没有完成,这利用context的WithTimeout()主动让goroutine退出
package mainimport ("fmt""time""context"
)// 场景: 如果你需要对一个用协程启动的函数做超时控制,可以用context来完成goroutine的控制func main()  {// 设置一个用于超时控制的context ctx, ctx作为参数可以用来作为协程的超时控制ctx,cancel := context.WithTimeout(context.Background(),10 * time.Second)defer cancel()// ctx作为参数传递给需要做超时控制的函数go Monitor(ctx)time.Sleep(20 * time.Second)
}func Monitor(ctx context.Context)  {for {select {// 如果context 超时,ctx.Done()就会返回一个空接口 struct{}case <- ctx.Done():// 如果超时时间到了,就退出循环fmt.Println(ctx.Err())return// 如果没有超时,打印输出后继续循环default:time.Sleep(1*time.Second)fmt.Println("monitor")}}
}

2.4 利用context向goroutine传递参数

  • 除了超时控制与主动停止goroutine,还有可以通过Context传递上下文变量给其他协程。这样可以避免在协程之间传递大量的变量,代码更整洁可维护。下面的例子通过WithValue传递给协程一个变量,并且通过channel在协程之间通信。
package mainimport ("context""fmt""time"
)func main() {// 为ctx设置一个key-valuectx := context.Background()ctx = context.WithValue(ctx, "hello", "world")x := ctx.Value("hello")fmt.Println("x=", x) // world// 将key-vluae值传递到goroutinego work(ctx)time.Sleep(3 * time.Second)}
func work(ctx context.Context) {fmt.Println("do worker.")fmt.Println("hello=", ctx.Value("hello")) // world,利用context传递key-value// 继续传递到下层goroutinego subwork(ctx)
}func subwork(ctx context.Context) {fmt.Println("do subwork.")fmt.Println("hello=", ctx.Value("hello")) // world,利用context传递key-value到更进一层
}

程序输出:

x= world
do worker.
world
do subwork.
world
http://www.hkea.cn/news/545292/

相关文章:

  • 怎样做自己的vip解析网站佛山外贸seo
  • 我的网站在百度搜不到了seo是什么职业做什么的
  • 网站私信界面国外网站seo免费
  • wordpress mysql类惠州网站seo
  • 为什么做网站必须要用域名举出最新的网络营销的案例
  • 电子请柬网站开发百度竞价推广登录入口
  • 网站设计与推广国际时事新闻2022最新
  • 柬埔寨网站开发营销技巧和营销方法
  • 网站建立价格长沙网站外包公司
  • 王建设医生个人网站免费google账号注册入口
  • 免费自建手机网站搜索引擎优化的方法包括
  • 甘肃省建设工程安全质量监督管理局网站官网拉新项目官方一手平台
  • 做电影网站赚钱武汉新闻最新消息
  • 做网站没有成本的方法上海百度分公司电话
  • 寺庙网站建设百度ai人工智能
  • 完成公司网站建设下载关键词推广软件
  • wordpress如何关闭网站下载app
  • WordPress小程序二次修改石家庄seo排名外包
  • 做百度关键词网站厦门seo外包
  • 泉州seo-泉州网站建设公司谷歌关键词搜索工具
  • 组织部网站建设方案行业关键词分类
  • 上海黄浦 网站制作中国搜索引擎排名2021
  • 手机网站建设 cms营销技巧和营销方法
  • 平顶山做网站优化微博搜索引擎优化
  • 网站如何做品牌宣传海报每日舆情信息报送
  • 做论坛网站需要多大空间seo推广招聘
  • 中国建设银行网站软件不限次数观看视频的app
  • 网站开发建设的步骤win11优化大师
  • 在线做数据图的网站樱桃bt磁力天堂
  • 网站建设费的税率东莞公司网上推广