Syncify with a Subprocess.
See original GitHub issueHi 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:
- 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);
}
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 runexec
commands the same way, since it doesn’t need to do the weird thing that exec currently does. That waystdin
/stdout
/stderr
aretty
’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:
- Created 8 years ago
- Comments:33 (33 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Mon 28th @
1 pm4 PM PSTRough agenda:
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.