2

I want to execute multiple processes concurrently from a lua script e.g.

os.execute("cmd1")
os.execute("cmd2")
os.execute("cmd3")

where cmd1,2 and 3 are continually running processes. When i do this as above, cmd2 and 3 will only run when cmd1 is finished. Any idea on this? Should i be using "fork" or something equivalent?

Thanks in advance

greatodensraven
  • 281
  • 1
  • 7
  • 14

4 Answers4

5

You've got several solutions to your problem:

  1. Depending on your operating system shell, you might use & to put tasks into the background. For example: os.execute('(sleep 10&& echo bar) & echo foo')
  2. Lua Posix comes with a posix.fork() function
  3. Lua Lanes gives you multithreading in Lua, so you might be able to just use os.execute in separate lanes (note that 'threads' in a Lua context usually refers to coroutines instead of native OS threads).
jpjacobs
  • 9,359
  • 36
  • 45
3

(answer mostly copied from Call popen with environment)

There is an os.spawn function in the ExtensionProposal API.

You can use it as follows:

require"ex"
local proc, err = os.spawn{
    command = e.."/bin/aprogr",
    args = {
        "arg1",
        "arg2",
        -- etc
    },
    env = {
        A = 100, -- I assume it tostrings the value
        B = "Hi",
        C = "Test",
    },
    -- you can also specify stdin, stdout, and stderr
    -- see the proposal page for more info
}
if not proc then
    error("Failed to aprogrinate! "..tostring(err))
end

-- if you want to wait for the process to finish:
local exitcode = proc:wait()

lua-ex-pai provides implementations for POSIX and Windows. It allows the spawning of multiple concurrent processes.

You can find precompiled binaries of this implementation bundled with the LuaForWindows distribution.

Here is a more concise version of your use case:

require"ex"
local cmd1_out = io.pipe()
local cmd1_proc = assert(os.spawn("cmd", {
    stdout = cmd1_out,
}))
local cmd2_out = io.pipe()
local cmd2_proc = assert(os.spawn("cmd", {
    stdout = cmd1_out,
}))
-- perform actions with cmd1 and cmd2
Community
  • 1
  • 1
Deco
  • 5,112
  • 1
  • 16
  • 17
  • Does lua-ex-api still maintained? I dont understand google code. Looks odd and old. Any hint on this? – Ismoh May 23 '22 at 08:42
2

That's because Lua is single threaded. To run it concurrently you'll need to provide a multi-threaded solution for Lua (not coroutines, because they're microthreads!), like lua pthreads.

Kornel Kisielewicz
  • 55,802
  • 15
  • 111
  • 149
2

Try simply adding & at the end of your commands:

os.execute("cmd1 &")
os.execute("cmd2 &")
os.execute("cmd3 &")

This should work on an operative system. On windows there might be a way to to the same, but I have no idea of what it is.

kikito
  • 51,734
  • 32
  • 149
  • 189