How can I run a method in a subprocess in golang?

251 views
Skip to first unread message

ggyft

unread,
Sep 21, 2018, 4:24:46 PM9/21/18
to golang-nuts

My use case is that I am designing a scheduler that schedules various health checks (go method) periodically. Earlier I want to model run them in a new goroutine. But I ran into problems when I want to design timeout for each method. Basically go seems not able to kill goroutine and clean up resources like socket etc.

I am wondering rather than run method in goroutine, can I run it in subprocess so I can kill it safely? My platform is windows hope that doesn't matter.

Justin Israel

unread,
Sep 21, 2018, 5:35:33 PM9/21/18
to ggyft, golang-nuts


On Sat, Sep 22, 2018, 8:24 AM 'ggyft' via golang-nuts <golan...@googlegroups.com> wrote:

My use case is that I am designing a scheduler that schedules various health checks (go method) periodically. Earlier I want to model run them in a new goroutine. But I ran into problems when I want to design timeout for each method. Basically go seems not able to kill goroutine and clean up resources like socket etc.


Rather than thinking about it as a lack of having a first class kill command for goroutines, you need to run your goroutines in a way that the work can be stopped. This can be accomplished with a Context that can be closed or times out, or a channel that receives a stop signal, or even an atomic that can be checked periodically. The work within the goroutine has to be aware of stop conditions. 

I am wondering rather than run method in goroutine, can I run it in subprocess so I can kill it safely? My platform is windows hope that doesn't matter.


There is nothing stopping you from running health checks as separate command as long as you have something to call. 

--
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.
Reply all
Reply to author
Forward
0 new messages