question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Syncify with a Subprocess.

See original GitHub issue

Hi Everyone,

I just wanted to open a discussion about something that I’ve been working on, before I work too hard on it.

I’ve talked about this before, but here’s my idea:

  • Because of the way Node works, it’s a lot easier to write things asyncly.
  • But we want shelljs commands to be sync.
  • Therefore, it would be nicer if we could syncify things,
  • However, this doesn’t work:
function syncSomething(arg1) {
    var ret = null, err = null, done = false;
    something(arg1, function (err, ret) {
        // We will never get here, because the event loop will never be clear, so the callback can never start.
        ret = _ret;
        err = _err;
        done = true;
    });
    while(!done) continue;
    if (err) throw err;
    return ret;
}
  • So, the only way to syncify (at least without a native extension) something is to make a second event loop.
  • The only way to do that is to start a second process.
  • So, what we could do is this:
  1. After all the argument parsing, common.wrap could do this:
// ...
if (callback || options.cp === false) {
    func.apply(null, args.concat([callback]));
} else {
    if (!this.cp) this.cp = new ChildProcess();
    return this.cp.cmd(this.name, args);
}
  1. ChildProcess.prototype.cmd could do this:
ChildProcess.prototype.cmd = function(cmd, args) {
    var id = Math.random().toString();
    write({ type: 'cmd', cmd, args, id }); // I've already written read/write, they work, so I won't go into it here.
    var ret, err, done;
    while (!done) {
        var events = read();
        for (var i = 0; i < events.length; ++i) {
            if (events[i].type === 'cmd' && events[i].id === id) {
                ret = events[i].ret;
                err = events[i].err;
                done = true;
            }
        }
    }
    if (err) throw err;
    return ret;
}
  • But since I realized that this was going to basically be a rewrite of shelljs, I thought we should discuss it first.
  • Also, @levithomason setup a really awesome build and lint system for shelljs/shx, so if we’re going to re-write it, I think we might want to get something similar. (@levithomason, would you be interested/willing in working this?)

Things this would fix (@shelljs/contributors, feel free to edit this and add stuff):

  • #412, #186 - We can spawn the subprocess with { stdio: 'inherit' }, and then it can run exec commands the same way, since it doesn’t need to do the weird thing that exec currently does. That way stdin/stdout/stderr are tty’s.
  • #2 - It’s easier to have an async version if you don’t have to maintain a sync version.
  • #3 - We can use packages that are stream-only.
  • #5, #167 - We don’t have to rely on a hacky wait loop.
  • #7
  • Maybe #158 - I think this might be caused by exec’s wait hack, or by improper cleanup.
  • #248

So, @shelljs/contributors and @shelljs/shx, what are your thoughts on this?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:33 (33 by maintainers)

github_iconTop GitHub Comments

1reaction
levithomasoncommented, Mar 24, 2016

Mon 28th @ 1 pm 4 PM PST

Plan next steps https://appear.in/shelljs

Rough agenda:

  • shipping 0.7, what’s left?
  • existing issues/PRs cleanup plan
  • sync/async plan
  • shx, what’s next?
0reactions
nfischercommented, Jan 16, 2017

I’m going to close this, since it would take too much effort to rewrite ShellJS. If someone really wants this, I think it’s best done as a fork of ShellJS.

Read more comments on GitHub >

github_iconTop Results From Across the Web

subprocess — Subprocess management — Python 3.11.1 ...
The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to ......
Read more >
How to synchronize the output of Python subprocess
I think I'm having issues to synchronize the output of two Popen running concurrently. It seems that the output from these two different ......
Read more >
The subprocess Module: Wrapping Programs With Python
In this tutorial, you'll learn how to leverage other apps and programs that aren't Python, wrapping them or launching them from your Python ......
Read more >
2 practical ways to use the Python subprocess module - Red Hat
This article will review two different use cases for the subprocess library: running simple Bash commands and running Bash scripts.
Read more >
An Introduction to Subprocess in Python With Examples
Subprocess in Python is used to run new codes by creating new processes. Learn how to use the Python subprocess module along with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found