is there anything in go equivalent to C's int fflush(FILE*stream) ?

1,710 views
Skip to first unread message

Hotei

unread,
Apr 19, 2012, 8:38:30 PM4/19/12
to golan...@googlegroups.com
I'm trying to run two processes concurrently.  One is a number cruncher, the other a "progress bar" that displays what percentage of the number cruncher is finished.  Since the number cruncher provides nothing in the way of status I'm estimating the run time via other means and just printing a '+' and sleeping a calculated amount for the status.  At the end of the progress bar function the number cruncher should be done  - or nearly so.  Anyway - in C or C++ this situation is usually resolved by flushing stdout.  Is there a way to do that explicitly in GO?  The os package doesn't seem to have anything appropriate listed under (*FIle) methods.

I'm open to other methods of doing this but so far this is pretty simple - at least in concept:

 // example: |++++++-------| shows about 50% percent estimated completion
go ProgressBar(200, 50) // task is expected to take 200 seconds, bar should be 50 characters wide
result, err := NumberCruncher()

Brad Fitzpatrick

unread,
Apr 19, 2012, 8:44:23 PM4/19/12
to Hotei, golan...@googlegroups.com
On Thu, Apr 19, 2012 at 5:38 PM, Hotei <hote...@gmail.com> wrote:
I'm trying to run two processes concurrently.

You mean goroutines, I think.  (looking at your code below)
 
 One is a number cruncher, the other a "progress bar" that displays what percentage of the number cruncher is finished.  Since the number cruncher provides nothing in the way of status I'm estimating the run time via other means and just printing a '+' and sleeping a calculated amount for the status.  At the end of the progress bar function the number cruncher should be done  - or nearly so.  Anyway - in C or C++ this situation is usually resolved by flushing stdout.  Is there a way to do that explicitly in GO?  The os package doesn't seem to have anything appropriate listed under (*FIle) methods. 

os.Stdout and Stderr aren't buffered in Go like they are in C.  (You can wrap them in bufio if you want that, not that you do)

 

I'm open to other methods of doing this but so far this is pretty simple - at least in concept:

 // example: |++++++-------| shows about 50% percent estimated completion
go ProgressBar(200, 50) // task is expected to take 200 seconds, bar should be 50 characters wide
result, err := NumberCruncher()

I bet you're running with GOMAXPROCS=1 and your progress bar goroutine can't run because you're crunching too hard in NumberCrunch.  You need to yield occasionally in your cruncher, or run with more GOMAXPROCS.

Jason Couture

unread,
Apr 19, 2012, 9:03:16 PM4/19/12
to golan...@googlegroups.com
I find the following line of code is suitable in most cases where I
need more than 1 thread.

runtime.GOMAXPROCS(runtime.NumCPU() + 1)

Hotei

unread,
Apr 19, 2012, 9:03:35 PM4/19/12
to golan...@googlegroups.com, Hotei
Brad,
Yes, goroutines, not processes.  On a go forum I should definitely use the lingo, especially when I know it (oops).

Hadn't thought of bufio - but you're right again.

And finally - you're right again!  With GOMAXPROCS = 2 it works perfectly.

Thanks for the super-quick response!  This forum is fantastic.

John Asmuth

unread,
Apr 19, 2012, 9:19:35 PM4/19/12
to golan...@googlegroups.com, Hotei
A way to avoid being dependent on GOMAXPROCS>1 is to insert carefully placed calls to runtime.Gosched(). 

Hotei

unread,
Apr 19, 2012, 9:45:06 PM4/19/12
to golan...@googlegroups.com, Hotei
John,
That's a good point, but I'd have to modify the number-cruncher rather than my own code. It's in a core go package so I wouldn't feel comfortable making a local change.  
Reply all
Reply to author
Forward
0 new messages