Live data from Hacker News

The Beauty of Concurrency in Go

pragprog.com

1–10 of 95 posts

Re: The Beauty of Concurrency in Go

#2
This is a nice article. I would encourage the author to use the term "goroutine" instead of "thread" when referring to Go's goroutines, because they aren't threads. The pros and cons of threads do not apply to goroutines. What's cool (for me) about this article is that the author knows this fact (and states so at the outset), but he re-discovers it and internalizes it over the course of his implementation.

Re: The Beauty of Concurrency in Go

#3
Ugh, I just finished writing the XMPP frontend for an XMPP/IRC bot I'm working on (http://www.getinstabot.com). The frontends are in Go for concurrency, and ferry messages back and forth from the channels to the backend.

Let me tell you, that problem is hard. Go coped pretty well, but the final thing is a mess of global states, and it's pretty elegant for what the problem is. I was hoping to avoid having many moving parts, but it ended up needing a lot of shared state between all processes.

Some problems are just hard, and, no matter how well-designed the language is, they'll still be hard. Something I miss from the language after implementing that is the ability to, say, monitor one goroutine from another to see if it returns (so the former can return as well). I know it's possible with channels, but when one goroutine is blocked on network Recv(), there's not much of a chance to listen to channels.

Anyway, yes, parallelism in Go is great, but not everything is magically all rainbows and unicorns (that's Django). Some problems will be messy and dirty, and even more so when you use channels.

Re: The Beauty of Concurrency in Go

#4
This has finally let me figure out what annoys me about Go, its a cargo-cult language. People saw that Erlang's Actors/processes were really popular and made it easy to write good, concurrent software.

They then went away and implemented their own language with lightweight processes and message passing, but missed the fact that actors are the price you have to pay for the benefits of not sharing mutable data.

And Go completely skipped that part (the most important part).

Re: The Beauty of Concurrency in Go

#5
post #4

This has finally let me figure out what annoys me about Go, its a cargo-cult language. People saw that Erlang's Actors/processes were really popular and made it easy to write good, concurrent software. They then went away and implemented their own language with lightweight processes and message passing, but missed the fact that actors are the price you have to pay for the benefits of not sharing mutable data. And Go…

Channels are basically queues with beefed up language support. Not sure what's so annoying about that considering it's a very common concurrency pattern across many languages.

Re: The Beauty of Concurrency in Go

#6
post #4

This has finally let me figure out what annoys me about Go, its a cargo-cult language. People saw that Erlang's Actors/processes were really popular and made it easy to write good, concurrent software. They then went away and implemented their own language with lightweight processes and message passing, but missed the fact that actors are the price you have to pay for the benefits of not sharing mutable data. And Go…

Doesn't go just implement Hoare's communicating sequential processes, as does Erlang? They share the same inspiration.

You don't need to share state data between your goroutines if you don't want to either just like you don't have to use mnesia to share state between erlang processes if you don't want to.

I don't think you can really accuse go of being a cargo cult language either, Rob Pike has implemented CSP multiple times (http://swtch.com/~rsc/thread/).

Re: The Beauty of Concurrency in Go

#7
The beauty of concurrency in Clojure:

; Rough sketch: def defines a var (pretend it's a reference)

; @ is used to dereference the future and block to wait for the result.

  (def f 
    (future 
      (Thread/sleep 10000) (println "done") 100))

  user=> @f
  done
  100

  ;; Dereferencing again will return the already calculated value.
  => @f
  100
http://clojuredocs.org/clojure_core/clojure.core/future

Edit: And more importantly, there are wrappers for the standard data structures designed around different concurrency use-cases (sync, async, coordinated, uncoordinated)

Refs are for Coordinated Synchronous access to Many Identities".

Atoms are for Uncoordinated synchronous access to a single Identity.

Agents are for Uncoordinated asynchronous access to a single Identity.

Vars are for thread local isolated identities with a shared default value.

http://stackoverflow.com/questions/9132346/clojure-differenc...

And you can use all (all!) of the Java concurrency tooling as desired, including raw threads (for which Clojure has a wrapper as well).

Part of the reason I use Clojure rather than Go is because it doesn't try to force you into a one-size-fits-all method for handling concurrency. I have no problem with CSP but it doesn't fit everything I do. Sometimes I just want to defer work or wrap it in a future. Or I want to use an intelligent coordinated data structure rather than trying to meld flesh and bone to steel in order to make a concurrency-naive data structure behave how I want in a concurrent environ.

If I can avoid those unnecessary battles, I will.

So - Clojure.

Re: The Beauty of Concurrency in Go

#8
This article isn't bad... but it misses several important points of Go. I also note the article is 9 months old. In the hope that my criticism will be taken as constructive, with apologies for not writing detailed explanations:

1. Goroutines are not threads

2. type inference allows you to elide types in var declarations: var host = flag.String(...

3. Go's convention is to use camel case, not underscores.

4. Calling os.Exit all over the place is unusual imo - it may be better to panic().

5. fmt.Fprintf exists os.Stderr.WriteString(fmt.Sprintf...

6. An explanation of why the standard log package isn't suitable would be nice, although I see the format used is slightly different.

7. Ignored errors when writing. Why do you wish to sync at every packet?

8. Massive race condition by re-using b. Line 62 overwrites b. b is then passed to c.logger (68) and c.binary_logger (70) for them to process asynchronously. c.logger is then passed another byte slice (72), which forces it to finish using b. c.binary_logger is not passed anything else, allowing it to delay until the next time something is sent on that channel, which would be after b is overwritten by the next packet. I think that simply moving line 58 to between 61,62 would fix this.

The author has not quite grokked the concept of "don't share memory". b is shared, and undefined behaviour results :(

Re: The Beauty of Concurrency in Go

#9
post #3

Ugh, I just finished writing the XMPP frontend for an XMPP/IRC bot I'm working on ( http://www.getinstabot.com ). The frontends are in Go for concurrency, and ferry messages back and forth from the channels to the backend. Let me tell you, that problem is hard . Go coped pretty well, but the final thing is a mess of global states, and it's pretty elegant for what the problem is. I was hoping to avoid having many movi…

> I know it's possible with channels, but when one goroutine is blocked on network Recv(), there's not much of a chance to listen to channels.

Could you elaborate a bit more on this point? This seems like a natural pattern for a channel/goroutine. Spin up a goroutine that reads from a connection and send whatever is read on a channel. Then other goroutines can synchronize on the channel.

Re: The Beauty of Concurrency in Go

#10
post #3

Ugh, I just finished writing the XMPP frontend for an XMPP/IRC bot I'm working on ( http://www.getinstabot.com ). The frontends are in Go for concurrency, and ferry messages back and forth from the channels to the backend. Let me tell you, that problem is hard . Go coped pretty well, but the final thing is a mess of global states, and it's pretty elegant for what the problem is. I was hoping to avoid having many movi…

heads up. Seems like https://getinstabot.com and http://www.getinstabot.com end up in a redirect loop.
Post reply on HN