Giter Club home page Giter Club logo

Comments (3)

squeek502 avatar squeek502 commented on September 24, 2024 1

I believe the biggest problem you're running into is that you don't have a call to uv.run() at the end of your scripts so the Libuv event loop never runs. This would be enough fix the code in the second comment, but the uv.spawn example from docs.md does seem to be broken.

The most up-to-date examples would be from our tests. Here's a relevant uv.spawn test:

luv/tests/test-process.lua

Lines 96 to 138 in 9f80386

test("process stdio", function (print, p, expect, uv)
local stdin = uv.new_pipe(false)
local stdout = uv.new_pipe(false)
local input = "Hello World"
local cmd, args, expectedOutput
if isWindows then
cmd = "cmd.exe"
args = {"/c", "set /p output=&call echo %output%"}
expectedOutput = input .. "\r\n"
else
cmd = "cat"
args = {"-"}
expectedOutput = input
end
local handle, pid
handle, pid = uv.spawn(cmd, {
args = args,
stdio = {stdin, stdout},
}, expect(function (code, signal)
p("exit", {code=code, signal=signal})
uv.close(handle)
end))
p{
handle=handle,
pid=pid
}
uv.read_start(stdout, expect(function (err, chunk)
p("stdout", {err=err,chunk=chunk})
assert(not err, err)
assert(chunk == expectedOutput)
uv.close(stdout)
end))
uv.write(stdin, input)
uv.shutdown(stdin, expect(function ()
uv.close(stdin)
end))
end)

Here's a full adapted version that will work outside our test setup:

local uv = require('luv')

-- set this depending on your OS
local isWindows = false

local stdin = uv.new_pipe(false)
local stdout = uv.new_pipe(false)

local input = "Hello World"
local cmd, args, expectedOutput
if isWindows then
  cmd = "cmd.exe"
  args = {"/c", "set /p output=&call echo %output%"}
  expectedOutput = input .. "\r\n"
else
  cmd = "cat"
  args = {"-"}
  expectedOutput = input
end

local handle, pid
handle, pid = uv.spawn(cmd, {
  args = args,
  stdio = {stdin, stdout},
}, function (code, signal)
  print("exit", code, signal)
  uv.close(handle)
end)

print(handle, pid)

uv.read_start(stdout, function (err, chunk)
  print("stdout", chunk)
  assert(not err, err)
  assert(chunk == expectedOutput)
  uv.close(stdout)
end)

uv.write(stdin, input)
uv.shutdown(stdin, function ()
  uv.close(stdin)
end)

uv.run()

from luv.

miversen33 avatar miversen33 commented on September 24, 2024

Additional notes, I also cannot get anything useful from the code snippet provided in uv.pipe

local uv = require("luv")
local fds = uv.pipe({nonblock=true}, {nonblock=true})

local read_pipe = uv.new_pipe()
read_pipe:open(fds.read)

local write_pipe = uv.new_pipe()
write_pipe:open(fds.write)

write_pipe:write("hello")
read_pipe:read_start(function(err, chunk)
  assert(not err, err)
  print(chunk)
end)

The above code does not fail, it just does nothing. I never see anything print from the read_pipe:read_start callback which leads me to believe this issue might be related to pipes in general, though I still don't know why they are failing here :/

I have verified that spawn does start the process (verified by doing, using it to modify my filesystem via mkdir). However, I cannot get output from the pipes

from luv.

miversen33 avatar miversen33 commented on September 24, 2024

Ahh it was uv.run that was getting me. There's a blurb about it in the tcp example at the top but I ignored the example as I wasn't interested in socket/network communication. Helps if you fully read the docs I suppose lol.

Thank you!

from luv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.