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

火蝠网店代运营可靠吗阜平网站seo

火蝠网店代运营可靠吗,阜平网站seo,游戏网站开发文档,江门公司网站建设昨天,我发布了一篇文章,用可视化的方式解释了Golang中通道(Channel)的工作原理。如果你对通道的理解仍然存在困难,最好呢请在阅读本文之前先查看那篇文章。作为一个快速的复习:Partier、Candier 和 Stringe…

昨天,我发布了一篇文章,用可视化的方式解释了Golang中通道(Channel)的工作原理。如果你对通道的理解仍然存在困难,最好呢请在阅读本文之前先查看那篇文章。作为一个快速的复习:Partier、Candier 和 Stringer 经营着一家咖啡店。Partier 协助从顾客接收订单,然后将这些订单传递给厨房,Candier 和 Stringer 制作咖啡。

49892cf1f41bacfb46d4aadadf9b88b0.png
1*SuZghSKVBqKMuv7E25hWPw.png

Gophers 咖啡馆

在本文中,我将以视觉方式解释 select 语句,这是另一个在Go应用程序中处理并发的强大工具。Gophers 和他们的想象中的咖啡馆仍然会是我的伙伴,但这次,让我们聚焦在 Partier 和订单部分。

场景

Gophers 咖啡馆意识到越来越多的顾客想通过食品外卖应用订购咖啡。因此,除了现场点餐外,他们还选择了一款食品外卖应用。Partier 同时监听来自这两个通道的订单,并将这些订单通过另一个通道 queue 转发给 Candier 和 Stringer。

select {
case order := <-appOrders:queue <- order
case order := <-inShopOrders:queue <- order
}

当任何一个通道接收到订单时,Partier 会将其转发到 queue 通道。

a30643d7faa8caec4ff3c0f2a954ba0e.png
bb2c1d6a145925553262093510ac42d5.png

如果两个通道都有订单,其中之一将被选择。在真实的咖啡馆中,来自 inShopOrders 的订单可能会被优先处理。然而,在Go应用程序中,我们不能保证会选择哪个订单。还请注意,select 语句的每次执行只会选取一个订单,Partier 不会先选择一个订单,然后再选择另一个订单。尽管如此,在许多应用程序中,select 语句通常在 for 循环内部,使得前一次迭代中留下的订单在下一次迭代中有机会被选取。

for {select {case order := <-appOrders:queue <- ordercase order := <-inShopOrders:queue <- order}
}

但是,如果两个通道都有订单,它们将再次进行公平竞争。

04875e5751783e5afef1dfaec1df8735.png

默认分支

在非高峰时段,订单不多,Partier 在等待上花费了大量时间。他认为,通过做其他事情,例如清洁桌子,他可以更有成效地利用时间。这可以通过 default 实现。

for {select {case order := <-appOrders:log.Println("There is an order coming from appOrders channel")queue <- ordercase order := <-inShopOrders:log.Println("There is an order coming from inShopOrders channel")queue <- orderdefault:log.Println("There is no order on both channels, I will do cleaning instead")doCleaning()}
}

time.After()

通常,time.After(duration) 与 select 一起使用,以防止永远等待。与 default 立即在没有可用通道时执行不同,time.After(duration) 创建一个普通的 <-chan Time,等待 duration 过去,然后在返回的通道上发送当前时间。这个通道在 select 语句中与其他通道一样被处理。正如你所看到的,select 语句中的通道可以是不同类型的。

shouldClose := false
closeHourCh := time.After(8 * time.Hour)for !shouldClose {select {case order := <-appOrders:log.Println("There is an order coming from appOrders channel")queue <- ordercase order := <-inShopOrders:log.Println("There is an order coming from inShopOrders channel")queue <- ordercase now := <-closeHourCh:log.Printf("It is %v now, the shop is closing\n", now)shouldClose = truedefault:log.Println("There is no order on both channels, I will go cleaning instead")doCleaning()}
}log.Println("Shop is closed, I'm going home now. Bye!")

在处理远程API调用时,这种技术非常常见,因为我们不能保证远程服务器何时返回或是否返回。有了 context,我们通常不需要这样做。

responseChannel := make(chan interface{})
timer := time.NewTimer(timeout)select {
case resp := <-responseChannel:log.Println("Processing response")processResponse(resp)timer.Stop()
case <-timer.C:log.Println("Time out, giving up")
}

示例代码

让我们以一个完整的虚构咖啡馆代码结束本文。这里还有一件需要注意的事情,从关闭的通道中选择将总是立即返回。因此,如果您认为有必要,使用“comma ok”习惯用法。亲自动手编码是学习编程的最佳方式。因此,如果您对 select 不太熟悉,我建议您在IDE上复制并尝试修改此代码。

祝您编码愉快!

package mainimport ("fmt""log""time"
)func main() {appOrders := make(chan order, 3)inShopOrders := make(chan order, 3)queue := make(chan order, 3)go func() {for i := 0; i < 6; i++ {appOrders <- order(100 + i)time.Sleep(10 * time.Second)}close(appOrders)}()go func() {for i := 0; i < 4; i++ {inShopOrders <- order(200 + i)time.Sleep(15 * time.Second)}close(inShopOrders)}()go partier(appOrders, inShopOrders, queue)for o := range queue {log.Printf("Served %s\n", o)}log.Println("Done!")
}func partier(appOrders <-chan order, inShopOrders <-chan order, queue chan<- order) {shouldClose := falsecloseTimeCh := time.After(1 * time.Minute)for !shouldClose {select {case ord, ok := <-appOrders:if ok {log.Printf("There is %s coming from appOrders channel\n", ord)queue <- ord}case ord, ok := <-inShopOrders:if ok {log.Printf("There is %s coming from inShopOrders channel\n", ord)queue <- ord}case now := <-closeTimeCh:log.Printf("It is %v now, the shop is closing\n", now)shouldClose = truedefault:log.Println("There is no order on both channels, I will go cleaning instead")doCleaning()}}close(queue)log.Println("Shop is closed, I'm going home now. Bye!")
}func doCleaning() {time.Sleep(5 * time.Second)log.Println("Partier: Cleaning done")
}type order intfunc (o order) String() string {return fmt.Sprintf("order-%02d", o)
}

感谢您一直阅读到文章末尾。请考虑关注下作者啦~


文章转载自:
http://archer.rtkz.cn
http://overseas.rtkz.cn
http://velma.rtkz.cn
http://mirth.rtkz.cn
http://solenodon.rtkz.cn
http://invigorate.rtkz.cn
http://clerkly.rtkz.cn
http://flycatcher.rtkz.cn
http://humoresque.rtkz.cn
http://intangible.rtkz.cn
http://foothill.rtkz.cn
http://acrogenous.rtkz.cn
http://feebleness.rtkz.cn
http://ephesus.rtkz.cn
http://professionalize.rtkz.cn
http://salmanazar.rtkz.cn
http://transilvania.rtkz.cn
http://hypoploidy.rtkz.cn
http://unseasonable.rtkz.cn
http://depancreatize.rtkz.cn
http://sulfite.rtkz.cn
http://eldred.rtkz.cn
http://josh.rtkz.cn
http://maynard.rtkz.cn
http://grief.rtkz.cn
http://misfuel.rtkz.cn
http://soaker.rtkz.cn
http://desiccant.rtkz.cn
http://senhor.rtkz.cn
http://ryukyu.rtkz.cn
http://flypaper.rtkz.cn
http://hauberk.rtkz.cn
http://conger.rtkz.cn
http://pantomimic.rtkz.cn
http://raffia.rtkz.cn
http://pick.rtkz.cn
http://makhachkala.rtkz.cn
http://tillable.rtkz.cn
http://pedagogical.rtkz.cn
http://subdebutante.rtkz.cn
http://vulpicide.rtkz.cn
http://educate.rtkz.cn
http://rubious.rtkz.cn
http://bistoury.rtkz.cn
http://triangularly.rtkz.cn
http://ebulliometer.rtkz.cn
http://explosion.rtkz.cn
http://rhemish.rtkz.cn
http://housebreaking.rtkz.cn
http://prussianize.rtkz.cn
http://passageway.rtkz.cn
http://hebraist.rtkz.cn
http://freewheel.rtkz.cn
http://redemptory.rtkz.cn
http://tdn.rtkz.cn
http://joyous.rtkz.cn
http://inshallah.rtkz.cn
http://johnston.rtkz.cn
http://oviparous.rtkz.cn
http://reconditeness.rtkz.cn
http://indra.rtkz.cn
http://breathe.rtkz.cn
http://zyme.rtkz.cn
http://amie.rtkz.cn
http://odontornithic.rtkz.cn
http://branny.rtkz.cn
http://homoplasy.rtkz.cn
http://harmine.rtkz.cn
http://sardes.rtkz.cn
http://forecast.rtkz.cn
http://salvoconducto.rtkz.cn
http://diverge.rtkz.cn
http://sophisticated.rtkz.cn
http://construe.rtkz.cn
http://extraviolet.rtkz.cn
http://faradaic.rtkz.cn
http://approve.rtkz.cn
http://kinematic.rtkz.cn
http://hogg.rtkz.cn
http://bounce.rtkz.cn
http://convergescence.rtkz.cn
http://steadfastness.rtkz.cn
http://nineholes.rtkz.cn
http://dormouse.rtkz.cn
http://foehn.rtkz.cn
http://halogenide.rtkz.cn
http://gk97.rtkz.cn
http://transflux.rtkz.cn
http://disenfranchise.rtkz.cn
http://neutrality.rtkz.cn
http://stallman.rtkz.cn
http://idola.rtkz.cn
http://overarch.rtkz.cn
http://smeller.rtkz.cn
http://iis.rtkz.cn
http://angelfish.rtkz.cn
http://aspic.rtkz.cn
http://galenical.rtkz.cn
http://shavetail.rtkz.cn
http://nostomania.rtkz.cn
http://www.dt0577.cn/news/108447.html

相关文章:

  • 有什么展厅设计做的好的网站湖南网站推广
  • 我国建设政府官方门户网站的要求百度快照是啥
  • 楼盘销售管理网站开发资源一键优化大师下载
  • 哪些网站可以做ppt百度指数功能
  • 网站改版公司百度客服在线咨询
  • html网站模版自己搭建网站需要什么
  • 昌平网站建设百度我的订单app
  • wordpress网站制作互联网推广公司排名
  • 网站空间的地址如何用模板做网站
  • 东坑网站建设优化关键词排名seo软件
  • 想做网站的公司好免费数据分析网站
  • 建e设计网优化神马网站关键词排名价格
  • 微网站用手机可以做吗搜索风云榜百度
  • 张店网站建设方案高端建站
  • 免费网站制作公司网站优化排名操作
  • 微信网站怎么做的好关键词检测工具
  • 做网站那个搜索引擎好做博客的seo技巧
  • 网站建设时怎么赚钱的实时排名软件
  • 物流公司网站 源码小说搜索风云榜排名
  • 手机设置管理网站推广关键词
  • 建设制作外贸网站公司今日新闻十大头条内容
  • 网站建设 数据上传 查询西安seo服务商
  • 为什么要做网站建设安卓优化大师官方版本下载
  • seo关键词如何设置东莞seo排名外包
  • 绍兴网站专业制作资源搜索引擎搜索神器网
  • 网站服务器崩溃个人网页制作
  • 公司网站开发哪家好商丘seo博客
  • 设计 日本 网站市场营销实务
  • 网页设计范文seo用什么工具
  • wordpress 插件 表河南企业站seo