lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 10 June 2015 at 07:55, Mike <lua-l@inbox.ru> wrote:
> Hello, everybody.
>
> Is there a way to run a piece of code from the currently executing lua script
> (I mean a lua chunk or a function) in a "subprocess" and establish a pipe
> to/from its stdin/stdout something like io.popen() does?
>
> This is somewhat different from the launching a subprocess for an external
> program or for the lua script itself that may be done with io.popen()
> as the launched chunk is to be able to access the current values of the
> currently executing lua script global variables and/or upvalues
> (at least their copies in the forked process's image).
> More simply, I mean the feature like "process substitution" in bash,
> for example:
> -----
> #! /bin/bash
>
> greeting="Hello world!"
>
> do-greeting()
> {
>   echo "$greeting"
> }
>
> cat < <(do-greeting)
> -----
>
> Thanks.
>
> --
> Mike
>

cqueues (http://25thandclement.com/~william/projects/cqueues.html)
includes a LWP (light weight process; aka thread) library:

local ct = require "cqueues.thread"
local thread, sock = ct.start(function(sock)
    sock:write("greeting\n")
end)
print(sock:read("*l"))