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

Returning Child with spawn #76

Open
npajkovsky opened this issue Oct 7, 2023 · 1 comment
Open

Returning Child with spawn #76

npajkovsky opened this issue Oct 7, 2023 · 1 comment

Comments

@npajkovsky
Copy link

Hey,

I'm enjoying your crate, but I hit the wall. Let's say, I would like to execute a command that outputs in threads

 all_servers.par_iter().try_for_each(op: |server: &Server| {
    cmd!(
        sh,
        "command with output"
    )

what happens that the output interleaves, and I have no way to know which thread outputs what. The only solution is to read the output and prefix it with Id, but that is not possible with your crate, right?

@matklad
Copy link
Owner

matklad commented Oct 10, 2023

Yeah, there's nothing built-in for this yet. What should be happening on the level of syscalls here is that each cmd! process should get its stdout/stderr redirected to a pipe (so, two pipes per process). Than, in the host process something should do an epoll loop over these pipes, reading the output and notifiying the user's code that "hey, process #k emitted output foo".

It's not at all clear how to express this functionality in xshell APIs (though it should be possible). A related thing here is that xshell already spawns thread behind user's back, so it won't be inconceivable to do more of that:

xshell/src/lib.rs

Line 1036 in ebe98b4

io_thread = Some(std::thread::spawn(move || {

I think the best API here, if we don't go full tokio/async would probably be something along the lines of

impl Cmd {
    fn stream_stderr(self, callback: impl FnMut(&mut [u8]) -> usize);
    fn stream_stderr_lines(self, callback: impl FnMut(&str));
}

Definitely in scope for the crate, but I am unlikely to work on it myself

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

No branches or pull requests

2 participants