Multithreading – golang blocking and non blocking
I'm a little confused about how to handle non - blocking io
Will go use to block IO when reading a file from a file or network? Or is there some magic to reproduce the code when used in the Go program?
It comes from the C # background, which is not intuitive. In C # we have a waiting keyword when using async API It clearly indicates that the API can generate the current thread and continue after continuing
So TLDR; When executing IO in the Go program, will the current thread be blocked, or will it be converted to C #, such as asynchronously waiting for the state machine to use continuation?
Solution
Go has a scheduler that allows you to write synchronous code, switch contexts by yourself, and use asynchronous io So if you run several goroutines, they may run on a single system thread. When your code blocks the view from goroutine, it is not really blocked It's not magic, but it covers up all your things
The scheduler will allocate system threads when needed and during operations that are really blocked (I think file IO, such as blocking or calling C code) However, if you are making some simple HTTP servers, you can use thousands of "real threads" to implement thousands of goroutines
You can read more about go's internal work:
https://morsmachine.dk/go-scheduler