Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a Non-Blocking Child Process Interface #211

Merged
merged 40 commits into from
Oct 16, 2024

Conversation

CompeyDev
Copy link
Contributor

@CompeyDev CompeyDev commented Jun 10, 2024

Design

As proposed previously, instead of using raw streams or events which tend to get complex or don't fit well within Luau, this design uses simple readers and writers for different streams which can be used at will.

local child = process.spawn(...)

local stdinWriter = child.stdin
local stdoutReader = child.stdout
local stderrReader = child.stderr

stdinWriter:write("hello")
stdinWriter:write(buffer.create(256))

while true do
	-- Reads a fixed length chunk at a time, returning a buffer of 8 bytes
	local buf = stdoutReader:read()
	
	-- When the entire buffer has been consumed, it returns an empty buffer
	if buffer.len(buf) == 0 then
		break
	end

	stdio.write(buffer.tostring(buf))
end

-- Yield until the child process exits and get its exit code
local status = child.status()
print(status.code)

if not status.ok then
	print(buffer.tostring(stderrReader:readToEnd())
end

Closes #200 and closes #131.

@CompeyDev CompeyDev marked this pull request as ready for review June 10, 2024 09:15
@CompeyDev
Copy link
Contributor Author

The PR is pretty much ready for review minus the windows test failing (due to the message not being written to stderr, hence the reader yielding).

crates/lune-std-process/Cargo.toml Outdated Show resolved Hide resolved
crates/lune-std-process/src/lib.rs Outdated Show resolved Hide resolved
tests/process/exec/basic.luau Outdated Show resolved Hide resolved
tests/process/spawn/non_blocking.luau Outdated Show resolved Hide resolved
crates/lune-std-process/src/stream.rs Outdated Show resolved Hide resolved
types/process.luau Outdated Show resolved Hide resolved
@filiptibell filiptibell merged commit 309c461 into lune-org:main Oct 16, 2024
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a way to process the stdout of a spawned process Provide a way to handle long-running processes
4 participants