Efficient way way to exec commands in parallel

1,254 views
Skip to first unread message

Rahul R

unread,
Nov 22, 2013, 2:36:26 PM11/22/13
to golang-nuts
What is the efficient way to execute commands in parallel in golang ?

I have the following code ,where i execute a bunch of commands using go routine. I do get the output of the commands executed. But I also get the following statements in the end. 

=====
throw: all goroutines are asleep - deadlock!

goroutine 1 [semacquire]:
sync.runtime_Semacquire(0x18600110, 0x18600110)
/build/buildd/golang-1/src/pkg/runtime/zsema_386.c:146 +0x29
sync.(*WaitGroup).Wait(0x1861c6a0, 0x3)
/usr/lib/go/src/pkg/sync/waitgroup.go:78 +0xe6
main.main()
/home/rahul/programs/Remodel/exec.go:27 +0x114

goroutine 2 [syscall]:
created by runtime.main
/build/buildd/golang-1/src/pkg/runtime/proc.c:221
======


I dont quite understand , whats happening. Why is it a deadlock at first place ? I am using a very wrong technique to parallize execution of commands ? If yes, whats the efficient way of doing so ?

./Rahul

Konstantin Kulikov

unread,
Nov 22, 2013, 2:42:51 PM11/22/13
to golan...@googlegroups.com
You forgot about wg.Done()

Carlos Castillo

unread,
Nov 22, 2013, 9:51:20 PM11/22/13
to golan...@googlegroups.com
Also, Add() should be incremented the amount of times you expect to call done. http://play.golang.org/p/3TDCiRWDU5

The reason why your code was deadlocking was because all the commands would finish, and so the only goroutine left was the main one, but it was waiting for the command goroutines to call wg.Done(). In go, if no goroutines can run, then you get a deadlock panic. 
Reply all
Reply to author
Forward
0 new messages