This is a creation in Article, where the information may have evolved or changed.
What's the simple next select?
Select is used to let our program monitor the processing mechanism of the state change of multiple file handles (Files descriptor). After you initiate some blocking requests, you can use the Select mechanism to scan the FD rotation until one or more of the monitored file handles has changed state.
The article wrote a little messy, welcome to spray! In addition, the article continues to update, please go to the original address to view the update.
http://xiaorui.cc/?p=2997
Originally just want to say select, but also can't help nonsense next said Epoll processing flow:
we can give the kernel (epoll_add) the file to be monitored for reading and writing, and the kernel will know the event notification in an interrupted way. It's a lot better than select. Set the events you care about (EPOLL_CTL), such as reading events. then Wait (epoll_wait), at this point, if no file has the event you care about, then hibernate until there is an event, be awakened, then return those events. The advantage of Epoll is that the OS that receives the data is responsible for notifying you that there is data that can be manipulated because the OS knows when the data is available. What is the connection between select and Golang? First, we learned from the above that select is a linear scan to monitor the file descriptor for changes. Channnel is also a file descriptor at the system level. In Golang we can use Goroutine to perform tasks concurrently, and then use Select to monitor the channel condition of each task. But if these tasks have not responded to the channel information for a long time, if we have a timeout request, then we can use a goroutine, this goroutine task logic to start sleep, After sleep, the channel signal is resumed.
Said so much useless, just talk to us golang goroutine Select Business ....
Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">#http://xiaorui.ccpackage mainimport ("FMT" "Time") Func main () {timeout: = make (chan bool, 1) go func () {time. Sleep (3 * time. Second)//Sleep 3 Second timeout <-true} () ch: = Make (Chan int.) Select {Case <-ch:case &L T;-timeout:fmt. PRINTLN ("task is timeout!") }} </textarea>
1234567891011121314151617181920212223 |
#http://xiaorui.cc PackageMainImport ( "FMT" "Time")funcMain() { Timeout := Make(ChanBOOL, 1) Gofunc() { Time.Sleep(3 * Time.Second) // Sleep 3 Second Timeout <- true }() CH := Make(Chanint) Select { Case <-CH: Case <-Timeout: FMT.Println("task is timeout!") }} |
The result of running the code above Golang is:
Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">task is timeout!</textarea>