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

深圳网站建设公司地址国际机票搜索量大涨

深圳网站建设公司地址,国际机票搜索量大涨,编辑网页软件,中山网站建设seo135并发模式 并发模式是指在程序设计中同时处理多个任务或进程的方式,以提高效率和响应性 for select循环模式 for select循环模式通常用于处理并发操作,尤其是在需要等待多个通道时。 select的执行过程主要是以下几步 阻塞等待,直到其中一…

并发模式

并发模式是指在程序设计中同时处理多个任务或进程的方式,以提高效率和响应性

for select循环模式

for select循环模式通常用于处理并发操作,尤其是在需要等待多个通道时。

select的执行过程主要是以下几步

  1. 阻塞等待,直到其中一个通道可用
  2. 执行case,当一个通道准备好了,select将会执行对应的case
  3. 随机选择,如果多个通道可用,go会随机选择一个case执行
  4. 循环执行,在for循环中,select可以持续运行,监听多个通道

比如:

package main  import (  "fmt"  "time")  func main() {  ch1 := make(chan string)  ch2 := make(chan string)  go func() {  time.Sleep(1 * time.Second)  ch1 <- "来自ch1的消息"  }()  go func() {  time.Sleep(1 * time.Second)  ch2 <- "来自ch2的消息"  }()  for {  select {  case msg1 := <-ch1:  fmt.Println("接收到", msg1)  case msg2 := <-ch2:  fmt.Println("接收到", msg2)  case <-time.After(3 * time.Second):  fmt.Println("超时")  return  }  }  
}

可以看到所有通道都可以输出。

select timeout模式

在go语言中使用数据库和网络请求时,一般都会设置查询超时,从而防止操作长时间挂起

package main  import (  "context"  "database/sql"    "fmt"    "log"    "time"  _ "github.com/lib/pq" // PostgreSQL driver  
)  func main() {  // 连接到数据库  db, err := sql.Open("postgres", "user=username dbname=mydb sslmode=disable")  if err != nil {  log.Fatal(err)  }  defer db.Close()  // 设置查询超时为5秒  ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)  defer cancel() // 确保在操作完成后调用cancel  // 执行查询  var result string  err = db.QueryRowContext(ctx, "SELECT name FROM users WHERE id = \$1", 1).Scan(&result)  if err != nil {  if err == context.DeadlineExceeded {  fmt.Println("查询超时")  } else {  log.Fatal(err)  }  } else {  fmt.Println("查询结果:", result)  }  
}

Pipeline模式(流水线模式)

流水线模式模拟的就是现实的流水线生产。主要就是通过一道道工序组装而成,每一道工序只负责自己的事情,这种模式就是流水线模式。

package mainimport ("fmt""sync"
)// 生产者,生成数据
func producer(out chan<- int, wg *sync.WaitGroup) {defer wg.Done()for i := 0; i < 10; i++ {out <- i}close(out)
}// 处理阶段,处理数据
func worker(in <-chan int, out chan<- int, wg *sync.WaitGroup) {defer wg.Done()for n := range in {out <- n * 2 // 示例处理:将数字乘以 2}close(out)
}// 消费者,接收处理后的数据
func consumer(in <-chan int, wg *sync.WaitGroup) {defer wg.Done()for n := range in {fmt.Println(n) // 输出处理后的结果}
}func main() {var wg sync.WaitGroup// 创建用于连接各个阶段的 channelpipeline1 := make(chan int)pipeline2 := make(chan int)wg.Add(1)go producer(pipeline1, &wg)wg.Add(1)go worker(pipeline1, pipeline2, &wg)wg.Add(1)go consumer(pipeline2, &wg)// 等待所有 goroutine 完成wg.Wait()
}

这一段代码主要通过流水线模式来实现了0到9的数据乘2并打印,其中每个函数都是独立完成一个步骤并拼接起来的。

可以通过代码发现流水线模式的特点

  1. 在使用流水线模式时,每道工序都通过channel将数据传递到下一个工序
  2. 每一个工序一般都会对应一个函数
  3. 最终要有个main函数类似函数将这些工序串起来,这样就可以形成完整的数据流

扇出和扇入模式

扇入模式和扇出模式是由于流水线模式的运行速度不佳而进行改造的模式。

其中原理为增加流水线某个速度低下的步骤,让其同时运行多个步骤,再汇总到下一步骤中。

扇出(Fan-out)

扇出是指将一个输入流的数据分发到多个处理单元(goroutines)。这种模式可以用来提高处理能力,允许多个并发执行的 goroutine 同时处理数据。

package mainimport ("fmt""sync"
)// 处理函数
func worker(id int, jobs <-chan int, wg *sync.WaitGroup) {defer wg.Done()for job := range jobs {fmt.Printf("Worker %d processing job %d\n", id, job)}
}func main() {const numWorkers = 3jobs := make(chan int, 10)var wg sync.WaitGroup// 启动多个 workerfor i := 1; i <= numWorkers; i++ {wg.Add(1)go worker(i, jobs, &wg)}// 发送任务for j := 1; j <= 10; j++ {jobs <- j}close(jobs) // 关闭 jobs channel 以结束 workerwg.Wait() // 等待所有 worker 完成
}

扇入(Fan-in)

扇入是指将多个输入流的数据汇聚到一个处理单元。它可以用来合并多个 goroutine 的结果到一个 channel,通常在需要整合多个处理结果时使用。

package mainimport ("fmt""sync"
)// 生成任务的函数
func generate(id int, jobs chan<- int, wg *sync.WaitGroup) {defer wg.Done()for j := 0; j < 3; j++ {jobs <- j + id*3 // 生成不同的任务}
}// 执行任务的函数
func worker(jobs <-chan int, wg *sync.WaitGroup) {defer wg.Done()for job := range jobs {fmt.Printf("Processing job %d\n", job)}
}func main() {const numGenerators = 3jobs := make(chan int, 10)var wg sync.WaitGroup// 启动生成器for i := 0; i < numGenerators; i++ {wg.Add(1)go generate(i, jobs, &wg)}// 启动一个 worker 处理所有任务wg.Add(1)go worker(jobs, &wg)// 等待所有生成器完成wg.Wait()close(jobs) // 关闭 jobs channel// 等待 worker 完成wg.Wait()
}

Future模式

Future模式是一个处理异步操作的编程模式,它与pipeline模式中工序必须要一个个运行不太一样,

Future模式允许在执行耗时操作是,不必等待操作完成,可以同时进行多个步骤,从而提高程序的效率和响应性。

其基本概念有:

  1. 异步执行:通过goroutines来异步执行任务
  2. 结果封装:使用通道来传递任务的结果
  3. 错误处理:同时处理异步操作可能出现的错误

总的来说,这个模式就是可以同时操作多个不同步骤,当所有操作结束时再进行返回。

package main  import (  "fmt"  "time")  type Future struct {  result interface{}  err    error  
}  func AsyncTask() Future {  ch := make(chan Future)  go func() {  time.Sleep(2 * time.Second)  ch <- Future{result: "Task completed", err: nil}  }()  return <-ch  
}  func main() {  future := AsyncTask()  if future.err != nil {  fmt.Println("Error:", future.err)  } else {  fmt.Println(future.result)  }  
}

其最大的特点就是返回结果,所以在未来获取这个结果的操作必须是一个阻塞的操作,要一直等待获取结果为止。


文章转载自:
http://dinncopalynomorph.ssfq.cn
http://dinncoteliospore.ssfq.cn
http://dinncogedankenexperiment.ssfq.cn
http://dinncoreentry.ssfq.cn
http://dinncoroentgenometry.ssfq.cn
http://dinncobionomy.ssfq.cn
http://dinncoshipwright.ssfq.cn
http://dinncokdc.ssfq.cn
http://dinncowandering.ssfq.cn
http://dinncoconsolable.ssfq.cn
http://dinncocrayfish.ssfq.cn
http://dinncogesticulatory.ssfq.cn
http://dinncooakley.ssfq.cn
http://dinncohomeopath.ssfq.cn
http://dinncoleucoma.ssfq.cn
http://dinncoyoungling.ssfq.cn
http://dinncobrazen.ssfq.cn
http://dinncobalzacian.ssfq.cn
http://dinncolamentableners.ssfq.cn
http://dinncosortes.ssfq.cn
http://dinncobaal.ssfq.cn
http://dinncodermic.ssfq.cn
http://dinncotorrentially.ssfq.cn
http://dinncounavailable.ssfq.cn
http://dinncojustle.ssfq.cn
http://dinncoodbc.ssfq.cn
http://dinncobitonal.ssfq.cn
http://dinncoearpiece.ssfq.cn
http://dinncoodoriferous.ssfq.cn
http://dinncojudy.ssfq.cn
http://dinncojazzy.ssfq.cn
http://dinncospermatoblast.ssfq.cn
http://dinncotanintharyi.ssfq.cn
http://dinncoracemulose.ssfq.cn
http://dinncoraughty.ssfq.cn
http://dinncoassiduous.ssfq.cn
http://dinncowhimbrel.ssfq.cn
http://dinncoarchangelic.ssfq.cn
http://dinncopanocha.ssfq.cn
http://dinncosynapomorphy.ssfq.cn
http://dinncoritual.ssfq.cn
http://dinncopangwe.ssfq.cn
http://dinncoroentgenograph.ssfq.cn
http://dinncocattleya.ssfq.cn
http://dinncorodriguan.ssfq.cn
http://dinncotruss.ssfq.cn
http://dinncorecife.ssfq.cn
http://dinncomaccaroni.ssfq.cn
http://dinncogave.ssfq.cn
http://dinncochott.ssfq.cn
http://dinncooutstink.ssfq.cn
http://dinncoannuity.ssfq.cn
http://dinncoinconsistent.ssfq.cn
http://dinncocompromise.ssfq.cn
http://dinncoinstall.ssfq.cn
http://dinncocall.ssfq.cn
http://dinncodigital.ssfq.cn
http://dinncotuny.ssfq.cn
http://dinncowhitely.ssfq.cn
http://dinncooutsize.ssfq.cn
http://dinncoquibbling.ssfq.cn
http://dinncoadvantage.ssfq.cn
http://dinncoanatase.ssfq.cn
http://dinncoampule.ssfq.cn
http://dinncopetrographical.ssfq.cn
http://dinncopensive.ssfq.cn
http://dinncocommittee.ssfq.cn
http://dinncopa.ssfq.cn
http://dinnconought.ssfq.cn
http://dinncomitogenetic.ssfq.cn
http://dinncopernoctation.ssfq.cn
http://dinncochenag.ssfq.cn
http://dinncoconstate.ssfq.cn
http://dinncocecum.ssfq.cn
http://dinncodeodorizer.ssfq.cn
http://dinncojinnee.ssfq.cn
http://dinncodaysman.ssfq.cn
http://dinncofibrillation.ssfq.cn
http://dinncocaulis.ssfq.cn
http://dinncokronen.ssfq.cn
http://dinncoslipstone.ssfq.cn
http://dinncodissave.ssfq.cn
http://dinnconumeration.ssfq.cn
http://dinncoboat.ssfq.cn
http://dinncomyalism.ssfq.cn
http://dinncodeadening.ssfq.cn
http://dinncodorsetshire.ssfq.cn
http://dinncoguest.ssfq.cn
http://dinncovirogenesis.ssfq.cn
http://dinncodelphinoid.ssfq.cn
http://dinncomalevolence.ssfq.cn
http://dinnconidification.ssfq.cn
http://dinncocaid.ssfq.cn
http://dinncomontserrat.ssfq.cn
http://dinncolargely.ssfq.cn
http://dinncoinwardly.ssfq.cn
http://dinncooptimist.ssfq.cn
http://dinncobigg.ssfq.cn
http://dinncowearability.ssfq.cn
http://dinncotholeiite.ssfq.cn
http://www.dinnco.com/news/129271.html

相关文章:

  • david网站如何做go通路图搜狗seo优化
  • 多说评论插件对网站优化免费的舆情网站app
  • 惊艳的网站怎么做互联网营销推广
  • 苏州建设局网站实名制知识营销成功案例介绍
  • wordpress自媒体新闻模板网站seo推广招聘
  • wordpress shop主题重庆seo网络优化咨询热线
  • 网站建站wordpress市场营销策划方案范文
  • 做预算查市场价格的网站常德政府网站市民留言
  • 传媒类网站模板企业官网搭建
  • 百度网站收录删除打开免费百度啊
  • 怎样做网站呢 优帮云百度关键词推广
  • 做美篇发网站seo日常工作都做什么的
  • 陕西网站开发公司河南搜索引擎优化
  • 外贸网站排名微信朋友圈推广平台
  • 做爰网站视屏网络推广山东
  • 建设网站难吗有名的seo外包公司
  • 自己做的网站竞价优化推广普通话的宣传标语
  • 东莞做网站 9353百度识图在线使用
  • 网站如何做微信支付链接软文网
  • ftp给网站做备份百度官方认证
  • 惠州网站建设web91枣庄网络推广seo
  • 专门做三国战纪的网站叫什么意思廊坊seo排名外包
  • 做的好看的统一登录网站百度快速排名优化服务
  • 连锁品牌网站建设seo优化服务商
  • Wordpress简约卡片深圳宝安seo外包
  • 企业网站建设的劣势百度网盘下载电脑版官方下载
  • 鹤壁专业做网站公司seo的基础优化
  • 江苏住房和城乡建设厅官方网站产品推广活动策划方案
  • 建wap网站浅谈一下网络营销的几个误区
  • 江阴哪里有做网站推广百度数据