Limit goroutine resource usage?

430 views
Skip to first unread message

Olivia Nelson

unread,
Aug 15, 2018, 11:29:44 PM8/15/18
to golang-nuts
I'm trying to limit the CPU usage of a goroutine. Is there anything like SuspendThread on windows and SIGSTOP/SIGCONTINUE on linux?

I'm not sure if it's possible.

andrey mirtchovski

unread,
Aug 15, 2018, 11:42:18 PM8/15/18
to the.warl...@gmail.com, golang-nuts
it seems from your suggested solutions that you're concerned about cpu
usage. does the goroutine communicate with anything? you can limit its
resources by feeding it less data (via a limiter goroutine) or by
editing it itself do less work (via editing the code)? there is
nothing inherently needed from the operating system to throttle it, it
can all be done via go communication patterns. for example we often
limit how much work can be done by using a n-sized buffered channel
that ensures only n items are processed at a certain time.
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Keith Randall

unread,
Aug 16, 2018, 7:41:59 PM8/16/18
to golang-nuts
There's no equivalent of the methods you mentioned.  You can't limit a goroutine from another goroutine, because (among other reasons) there's no way to name it.
A goroutine can limit itself a bit by calling runtime.Gosched, or more forcefully using time.Timer.
Andrey's suggestion of limiting how fast you feed it work is also a good idea.
Reply all
Reply to author
Forward
0 new messages