dssqq82402 2018-10-25 13:45
浏览 381
已采纳

go语句之间的睡眠语句

I saw the below setup in a code base today. Does the Sleep() function have any effect? I suppose it's there to guarantee that serverA is running before serverB starts, but since execution of both may be deferred until later, this is not really guaranteed, right?

func main() {

    go util.Execute("run", serverA)

    time.Sleep(time.Millisecond * 500)

    go util.Execute("run", serverB)

    for {
        log.Println("running")
        time.Sleep(time.Second * 30)
    }
}
  • 写回答

1条回答 默认 最新

  • dsla94915 2018-10-25 13:58
    关注

    It does have an effect that it will wait 500 ms before the next goroutine will attempt to run and start serverB. And you're correct that it's not really guaranteed that serverA will completely start within that time.

    A better strategy would be to check for some kind of signal from serverA that it is fully running, but not knowing anything about what type of server that is it would be hard to advise.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度