Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Go, does it make sense to write non-blocking code?

coming from node.js point of view, where all code is non-blocking.

In Go, non-blocking is easily achieved using channels.

if one were writing a node.js type server in go, does it make sense to make it non-blocking? for example, having a database connect() function return a channel, as versus blocking while waiting for the connection to occur.

to me, this seems the correct approach

but ...

like image 597
cc young Avatar asked Jun 13 '11 09:06

cc young


People also ask

Is go non-blocking?

Go has a scheduler that lets you write synchronous code, and does context switching on its own and uses async IO under the hood. So if you're running several goroutines, they might run on a single system thread, and when your code is blocking from the goroutine's view, it's not really blocking.

Are Go routines blocking?

When we send data into the channel using a GoRoutine, it will be blocked until the data is consumed by another GoRoutine. When we receive data from channel using a GoRoutine, it will be blocked until the data is available in the channel.

Is Go synchronous or asynchronous?

What is Geosynchronous Orbit ? These satellites are synchronous with respect to the Earth. This means looking from a fixed point from earth, these satellites appear to be stationary. This satellite covers around 1/3 (i.e. 120 degrees latitude) of the Earth's surface.

What is non-blocking code?

Blocking refers to operations that block further execution until that operation finishes while non-blocking refers to code that doesn't block execution. Or as Node. js docs puts it, blocking is when the execution of additional JavaScript in the Node.


2 Answers

Blocking and non-blocking aren't really about performance, they are about an interface. If you have a single thread of execution then a blocking call prevents your program from doing any useful work while it's waiting. But if you have multiple threads of execution a blocking call doesn't really matter because you can just leave that thread blocked and do useful work in another.

In Go, a goroutine is swapped out for another one when it blocks on I/O. The Go runtime uses non-blocking I/O syscalls to avoid the operating system blocking the thread so a different goroutine can be run on it while the first is waiting for it's I/O.

Goroutines are really cheap so writing non-blocking style code is not needed.

like image 175
Jesse Avatar answered Oct 10 '22 17:10

Jesse


Write blocking functions. The language allows you to easily turn a synchronous call into an asynchronous one.

If you want to call a function asynchronously, use a go statement. Something like this:

c := make(chan bool)
go func() {
    blockingFunction()
    c <- true
}()

// do some other stuff here while the blocking function runs

// wait for the blocking function to finish if it hasn't already
<-c
like image 32
Evan Shaw Avatar answered Oct 10 '22 16:10

Evan Shaw