<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 處理信號量 信號是用戶或操作系統殺死正在運行的應用程序的有效方式。有時,以更優雅的方式處理這些信號是有意義的。Go提供了一種捕獲和處理信號的機制。在本節中,我們將通過使用Go例程的信號來討論信號的處理。 ### 實踐 在通道的使用中,done的使用很常見,參見[Concurrency in Go 中文筆記 or-done-channel](http://www.hmoore.net/mutouzhang/go/596845) 1. 建立signals.go: ``` package main import ( "fmt" "os" "os/signal" "syscall" ) // CatchSig 為SIGINT中斷設置一個監聽器 func CatchSig(ch chan os.Signal, done chan bool) { // 在等待信號時阻塞 sig := <-ch // 當接收到信號時打印 fmt.Println("\nsig received:", sig) // 對信號類型進行處理 switch sig { case syscall.SIGINT: fmt.Println("handling a SIGINT now!") case syscall.SIGTERM: fmt.Println("handling a SIGTERM in an entirely different way!") default: fmt.Println("unexpected signal received") } // 終止 done <- true } func main() { // 初始化通道 signals := make(chan os.Signal) done := make(chan bool) // 將它們連接到信號lib signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) // 如果一個信號被這個go例程捕獲,它將寫入 done go CatchSig(signals, done) fmt.Println("Press ctrl-c to terminate...") // 程序會持續打印日志直到done通道被寫入 <-done fmt.Println("Done!") } ``` 2. 運行并按下ctrl+c,會顯示: ``` Press ctrl-c to terminate... ^C sig received: interrupt handling a SIGINT now! Done! ``` 3. 嘗試在另一個單獨的命令行窗口運行,然后殺掉其PID,會顯示: ``` $./signals Press ctrl-c to terminate... # in a separate terminal $ ps -ef | grep signals 501 30777 26360 0 5:00PM ttys000 0:00.00 ./signals $ kill -SIGTERM 30777 # in the original terminal sig received: terminated handling a SIGTERM in an entirely different way! Done! ``` ### 說明 本節使用了通道,第9章“并行和并發”會對此進行了更廣泛的介紹。signal.Notify函數需要通道以發送信號通知。在命令行殺掉應用是測試向應用程序傳遞信號的好方法。我們用信號記錄程序關心的信號類型。然后,在Go例程中處理傳遞給該函數的通道上的活動。一旦我們收到信號,就可以隨心所欲地處理它。我們可以終止應用程序,使用消息進行響應,并針對不同的信號實施不同的行為。 我們還使用done通道阻止應用程序終止,直到收到信號。對于長期運行的應用程序(如Web應用程序),這是不必要的。創建適當的信號處理例程以進行清理非常有用,尤其是在持有大量狀態的應用程序中。例如,在正常關閉時,允許當前處理程序完成其HTTP請求而非中途終止它們。 * * * * 學識淺薄,錯誤在所難免。歡迎在群中就本書提出修改意見,以饗后來者,長風拜謝。 Golang中國(211938256) beego實戰(258969317) Go實踐(386056972)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看