FAQ
Hi All,

I'm new to the Go programming language and so far I really like it.
I'm currently trying to build Go bindings for Spread but i'm stuck at from my point of view a very simple thing:

I want my test program to run an poll Spread for new messages.

So what I need if something like this

for {
n := C.SP_poll(mbox)
// if n is have something useful do something useful...
}

The problem is I get at least one CPU going to 100 percent.

So I started the read a lot about this and I see solutions like setting the GOMAXPROCS but then
I got multiple CPU doing something but I should not be that busy at all.

The C variant uses 0.0 CPU and thats more like it !

So far I have tried about every solution I can find but the best I could get was about 15% cpu use.
This was when I said to Go time.Sleep(whatever)

All the examples I have found and the videos i have watched are using example which uses
a for loop with a maximum of some number of cycles.

But I can't figure a way to get my application running not eating all my CPU just being there in the background polling to wait for something to do.

So I would be very glad with a proper solution so any advice would be very welcome !

Thanks,

Jeroen







--

Search Discussions

  • Tamás Gulácsi at Jan 20, 2013 at 5:41 pm
    Use tim.Sleep(1*time.Minute)

    --
  • Steve wang at Jan 20, 2013 at 6:09 pm
    From Go's perspective(IMO), your C.SP_poll() is supposed to wait until
    something happens rather than just return empty-handed, which can prevent
    your program from hogging all the cpu time.
    On Sunday, January 20, 2013 8:38:23 PM UTC+8, Jeroen Grusewski wrote:


    Hi All,

    I'm new to the Go programming language and so far I really like it.
    I'm currently trying to build Go bindings for Spread but i'm stuck at from
    my point of view a very simple thing:

    I want my test program to run an poll Spread for new messages.

    So what I need if something like this

    for {
    n := C.SP_poll(mbox)
    // if n is have something useful do something useful...
    }

    The problem is I get at least one CPU going to 100 percent.

    So I started the read a lot about this and I see solutions like setting
    the GOMAXPROCS but then
    I got multiple CPU doing something but I should not be that busy at all.

    The C variant uses 0.0 CPU and thats more like it !

    So far I have tried about every solution I can find but the best I could
    get was about 15% cpu use.
    This was when I said to Go time.Sleep(whatever)

    All the examples I have found and the videos i have watched are using
    example which uses
    a for loop with a maximum of some number of cycles.

    But I can't figure a way to get my application running not eating all my
    CPU just being there in the background polling to wait for something to do.

    So I would be very glad with a proper solution so any advice would be very
    welcome !

    Thanks,

    Jeroen






    --
  • Jeroen Grusewski at Jan 20, 2013 at 6:11 pm
    The problem is not with the SP_poll even an empty for loop is taking all
    the cpu...

    Op 20 jan. 2013 om 19:09 heeft steve wang <steve.wangbin@gmail.com> het
    volgende geschreven:

    From Go's perspective(IMO), your C.SP_poll() is supposed to wait until
    something happens rather than just return empty-handed, which can prevent
    your program from hogging all the cpu time.
    On Sunday, January 20, 2013 8:38:23 PM UTC+8, Jeroen Grusewski wrote:


    Hi All,

    I'm new to the Go programming language and so far I really like it.
    I'm currently trying to build Go bindings for Spread but i'm stuck at from
    my point of view a very simple thing:

    I want my test program to run an poll Spread for new messages.

    So what I need if something like this

    for {
    n := C.SP_poll(mbox)
    // if n is have something useful do something useful...
    }

    The problem is I get at least one CPU going to 100 percent.

    So I started the read a lot about this and I see solutions like setting
    the GOMAXPROCS but then
    I got multiple CPU doing something but I should not be that busy at all.

    The C variant uses 0.0 CPU and thats more like it !

    So far I have tried about every solution I can find but the best I could
    get was about 15% cpu use.
    This was when I said to Go time.Sleep(whatever)

    All the examples I have found and the videos i have watched are using
    example which uses
    a for loop with a maximum of some number of cycles.

    But I can't figure a way to get my application running not eating all my
    CPU just being there in the background polling to wait for something to do.

    So I would be very glad with a proper solution so any advice would be very
    welcome !

    Thanks,

    Jeroen






    --
  • Jan Mercl at Jan 20, 2013 at 6:17 pm

    On Sun, Jan 20, 2013 at 7:11 PM, Jeroen Grusewski wrote:
    The problem is not with the SP_poll even an empty for loop is taking all the
    cpu...
    An empty for loop is expected to take 100% CPU.

    -j

    --
  • Jeroen Grusewski at Jan 20, 2013 at 6:24 pm
    I can understand but what is the best solution is this case ?

    Op 20 jan. 2013 om 19:17 heeft Jan Mercl <0xjnml@gmail.com> het
    volgende geschreven:
    On Sun, Jan 20, 2013 at 7:11 PM, Jeroen Grusewski wrote:
    The problem is not with the SP_poll even an empty for loop is taking all the
    cpu...
    An empty for loop is expected to take 100% CPU.

    -j
    --
  • Jan Mercl at Jan 20, 2013 at 6:28 pm

    On Sun, Jan 20, 2013 at 7:19 PM, Jeroen Grusewski wrote:
    I can understand but what is the best solution is this case ?
    I can think only of either hooking to some callback or calling a
    blocking function instead (if anything like that exists at the C
    side). Or: can be kernel epoll used (I don't know what SP_poll is)?

    -j

    --
  • Rémy Oudompheng at Jan 20, 2013 at 6:34 pm

    On 2013/1/20 Jeroen Grusewski wrote:
    I can understand but what is the best solution is this case ?
    You have mentioned that you had a working C version. What does it look
    like? You may use the same techniques in Go.

    --
  • Steve wang at Jan 20, 2013 at 6:23 pm
    Absolutely an empty loop will exhaust your CPU.
    You'd better check your functions called in the loop and make sure they get
    something to await.
    On Monday, January 21, 2013 2:11:44 AM UTC+8, Jeroen Grusewski wrote:

    The problem is not with the SP_poll even an empty for loop is taking all
    the cpu...

    Op 20 jan. 2013 om 19:09 heeft steve wang <steve....@gmail.com<javascript:>>
    het volgende geschreven:

    From Go's perspective(IMO), your C.SP_poll() is supposed to wait until
    something happens rather than just return empty-handed, which can prevent
    your program from hogging all the cpu time.
    On Sunday, January 20, 2013 8:38:23 PM UTC+8, Jeroen Grusewski wrote:


    Hi All,

    I'm new to the Go programming language and so far I really like it.
    I'm currently trying to build Go bindings for Spread but i'm stuck at
    from my point of view a very simple thing:

    I want my test program to run an poll Spread for new messages.

    So what I need if something like this

    for {
    n := C.SP_poll(mbox)
    // if n is have something useful do something useful...
    }

    The problem is I get at least one CPU going to 100 percent.

    So I started the read a lot about this and I see solutions like setting
    the GOMAXPROCS but then
    I got multiple CPU doing something but I should not be that busy at all.

    The C variant uses 0.0 CPU and thats more like it !

    So far I have tried about every solution I can find but the best I could
    get was about 15% cpu use.
    This was when I said to Go time.Sleep(whatever)

    All the examples I have found and the videos i have watched are using
    example which uses
    a for loop with a maximum of some number of cycles.

    But I can't figure a way to get my application running not eating all my
    CPU just being there in the background polling to wait for something to do.

    So I would be very glad with a proper solution so any advice would be
    very welcome !

    Thanks,

    Jeroen






    --
  • Guillermo Estrada at Jan 20, 2013 at 7:01 pm
    I think the proper Go solution will be the use of channels and goroutines
    as workers polling a channel of the desired type. When you have for example
    a for loop in range of a channel, it will block until it receives something
    to work with (I have to try of this consumes CPU at all, bit should be
    minimal), and you can actually make that multithreaded by creating workers.
    I hope this was helpful, you Just have to make tour poll function be a
    channel, and a goroutine putting things on the channel, it is also a more
    idiomatic way to do it. More people here are suites for a code example.

    On Sunday, January 20, 2013 6:38:23 AM UTC-6, Jeroen Grusewski wrote:


    Hi All,

    I'm new to the Go programming language and so far I really like it.
    I'm currently trying to build Go bindings for Spread but i'm stuck at from
    my point of view a very simple thing:

    I want my test program to run an poll Spread for new messages.

    So what I need if something like this

    for {
    n := C.SP_poll(mbox)
    // if n is have something useful do something useful...
    }

    The problem is I get at least one CPU going to 100 percent.

    So I started the read a lot about this and I see solutions like setting
    the GOMAXPROCS but then
    I got multiple CPU doing something but I should not be that busy at all.

    The C variant uses 0.0 CPU and thats more like it !

    So far I have tried about every solution I can find but the best I could
    get was about 15% cpu use.
    This was when I said to Go time.Sleep(whatever)

    All the examples I have found and the videos i have watched are using
    example which uses
    a for loop with a maximum of some number of cycles.

    But I can't figure a way to get my application running not eating all my
    CPU just being there in the background polling to wait for something to do.

    So I would be very glad with a proper solution so any advice would be very
    welcome !

    Thanks,

    Jeroen






    --
  • Jeroen Grusewski at Jan 20, 2013 at 10:21 pm
    I get what u mean. Do you know a good example somewhere ?

    Op 20 jan. 2013 om 20:01 heeft Guillermo Estrada <phrozen10@gmail.com> het
    volgende geschreven:

    I think the proper Go solution will be the use of channels and goroutines
    as workers polling a channel of the desired type. When you have for example
    a for loop in range of a channel, it will block until it receives something
    to work with (I have to try of this consumes CPU at all, bit should be
    minimal), and you can actually make that multithreaded by creating workers.
    I hope this was helpful, you Just have to make tour poll function be a
    channel, and a goroutine putting things on the channel, it is also a more
    idiomatic way to do it. More people here are suites for a code example.

    On Sunday, January 20, 2013 6:38:23 AM UTC-6, Jeroen Grusewski wrote:


    Hi All,

    I'm new to the Go programming language and so far I really like it.
    I'm currently trying to build Go bindings for Spread but i'm stuck at from
    my point of view a very simple thing:

    I want my test program to run an poll Spread for new messages.

    So what I need if something like this

    for {
    n := C.SP_poll(mbox)
    // if n is have something useful do something useful...
    }

    The problem is I get at least one CPU going to 100 percent.

    So I started the read a lot about this and I see solutions like setting
    the GOMAXPROCS but then
    I got multiple CPU doing something but I should not be that busy at all.

    The C variant uses 0.0 CPU and thats more like it !

    So far I have tried about every solution I can find but the best I could
    get was about 15% cpu use.
    This was when I said to Go time.Sleep(whatever)

    All the examples I have found and the videos i have watched are using
    example which uses
    a for loop with a maximum of some number of cycles.

    But I can't figure a way to get my application running not eating all my
    CPU just being there in the background polling to wait for something to do.

    So I would be very glad with a proper solution so any advice would be very
    welcome !

    Thanks,

    Jeroen






    --
  • Dave Cheney at Jan 20, 2013 at 10:23 pm
    Is C.SP_poll(mbox) returning an error? That may explain why it spins in a loop.

    Try n, err := C.SP_poll(mbox) and check the value of err (as errno) to
    see what is going on. You can probably also try to strace your
    process, you should see some syscalls coming out of the C code which
    may give a hint why it is spinning.
    On Sun, Jan 20, 2013 at 11:38 PM, Jeroen Grusewski wrote:

    Hi All,

    I'm new to the Go programming language and so far I really like it.
    I'm currently trying to build Go bindings for Spread but i'm stuck at from my point of view a very simple thing:

    I want my test program to run an poll Spread for new messages.

    So what I need if something like this

    for {
    n := C.SP_poll(mbox)
    // if n is have something useful do something useful...
    }

    The problem is I get at least one CPU going to 100 percent.

    So I started the read a lot about this and I see solutions like setting the GOMAXPROCS but then
    I got multiple CPU doing something but I should not be that busy at all.

    The C variant uses 0.0 CPU and thats more like it !

    So far I have tried about every solution I can find but the best I could get was about 15% cpu use.
    This was when I said to Go time.Sleep(whatever)

    All the examples I have found and the videos i have watched are using example which uses
    a for loop with a maximum of some number of cycles.

    But I can't figure a way to get my application running not eating all my CPU just being there in the background polling to wait for something to do.

    So I would be very glad with a proper solution so any advice would be very welcome !

    Thanks,

    Jeroen







    --
    --
  • Jan Mercl at Jan 20, 2013 at 10:32 pm

    On Sun, Jan 20, 2013 at 11:23 PM, Dave Cheney wrote:
    Is C.SP_poll(mbox) returning an error? That may explain why it spins in a loop.

    Try n, err := C.SP_poll(mbox) and check the value of err (as errno) to
    see what is going on.
    Calling a C function can return also the errno as a second return
    value? A mistake or a nice property of CGO I'm not aware of?

    -j

    --
  • Phil Pennock at Jan 20, 2013 at 10:58 pm

    On 2013-01-20 at 23:32 +0100, Jan Mercl wrote:
    Calling a C function can return also the errno as a second return
    value? A mistake or a nice property of CGO I'm not aware of?
    The latter.

    http://golang.org/cmd/cgo/

    Any C function that returns a value may be called in a multiple
    assignment context to retrieve both the return value and the C errno
    variable as an error. For example:

    n, err := C.atoi("abc")

    --
  • Kevin Gillette at Jan 20, 2013 at 11:02 pm
    Gee, then next time I want to write C, I'll just embed it in Go.

    --
  • Minux at Jan 21, 2013 at 4:34 am

    On Monday, January 21, 2013, Phil Pennock wrote:
    On 2013-01-20 at 23:32 +0100, Jan Mercl wrote:
    Calling a C function can return also the errno as a second return
    value? A mistake or a nice property of CGO I'm not aware of?
    The latter.

    http://golang.org/cmd/cgo/

    Any C function that returns a value may be called in a multiple
    Go 1.1 will support getting errno even for void C functions,
    so the docs needs to be updated.
    assignment context to retrieve both the return value and the C errno
    variable as an error. For example:

    n, err := C.atoi("abc")
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 20, '13 at 5:02p
activeJan 21, '13 at 4:34a
posts16
users10
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase