Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to: Communicate with a subprocess

Tags:

go

lua

ipc

I have a Go program that starts a C/Lua process. Now I'd like to communicate between those two. For example, in the middle of the run of the child (the c/lua process), I'd like to ask the parent (the Go program) to do some calculations and wait for the result. I am not keen to use stdin/stdout for communication, as this is already used for regular output. Now I am thinking of using sockets for the communication, but I don't want to reinvent the wheel.

  • What are the obvious choices for this kind of communication?
  • Is there a (more or less) simple standard way to pass objects between Lua and Go? If not, text blobs would suffice.
  • Are Protocol Buffers suitable for this? Looks like overkill, but I have no experience here.
like image 987
topskip Avatar asked Dec 11 '25 05:12

topskip


1 Answers

Besides all the usual IPC methods you've mentioned (yeah, a unix socket with protobuf should do it, and stdin/stdout as well), if you run the C/Lua code embedded in your program, and not start it as a process, you can actually communicate between the languages directly.

Using the cgo module, Go code can call C functions, and embedded C code can call Go functions. See here: http://golang.org/cmd/cgo/#hdr-C_references_to_Go

Also, you have a couple embeddable Lua binding libraries for Go which you can try, that let you call Lua code and let your Lua code call Go. see https://github.com/aarzilli/golua and https://github.com/stevedonovan/luar/

like image 97
Not_a_Golfer Avatar answered Dec 14 '25 00:12

Not_a_Golfer