Coder Social home page Coder Social logo

Add `.fork()` method about execa HOT 13 CLOSED

sindresorhus avatar sindresorhus commented on September 24, 2024
Add `.fork()` method

from execa.

Comments (13)

sindresorhus avatar sindresorhus commented on September 24, 2024

but that would mean not capitalising on the added optimization that happens when using fork, as mentioned here.

That is, as always with Stack Overflow answers, incorrect. .fork is just .spawn with an IPC channel. You can easily do that with .spawn too, by specifying it in the stdio option.

We should definitely add .fork() here though, as it's handy. I was thinking about it initially, but wanted to explore ways of making .fork() better and never got to it.

Pull request welcome :)

from execa.

sindresorhus avatar sindresorhus commented on September 24, 2024

If anyone wants to take on this, see #108 for context and previous work.

from execa.

IssueHuntBot avatar IssueHuntBot commented on September 24, 2024

@IssueHunt has funded $60.00 to this issue.


from execa.

ehmicky avatar ehmicky commented on September 24, 2024

Just to add to the discussion, I don't think there is any "added optimization" with fork(). If you check the Node.js source code, fork() only performs some normalization logic then calls spawn(). The extra logic introduced by fork() is:

  • use process.execPath (i.e. node) as the main binary. I.e. fork('file.js') becomes spawn('node', ['file.js']). process.execPath can be modified with the execPath option.
  • use process.execArgv (i.e. node CLI arguments). I.e. if node was fired with --no-warnings, fork('file.js') becomes spawn('node', ['--no-warnings', 'file.js']). process.execArgv can be modified with the execArgv option.
  • add a silent option which is the same as using stdio: 'pipe'.
  • always use shell: false option.

So there are no optimizations. However this is convenient for two reasons:

  • it's shorter because node does not need to passed as argument
  • it re-uses the current node path (if several node binaries are installed) and CLI flags.

from execa.

sindresorhus avatar sindresorhus commented on September 24, 2024

@ehmicky Yes, it's mostly for convenience, but also:

The returned ChildProcess will have an additional communication channel built-in that allows messages to be passed back and forth between the parent and child.

from execa.

ehmicky avatar ehmicky commented on September 24, 2024

Oh yes you're right.

This could be seen as a convenience as well since this can be achieved with spawn() by adding ipc to the stdio option, e.g. stdio: ['inherit', 'inherit', 'inherit', 'ipc'] (which is what actually fork() does under the hood).

Also to my above points, we should add that fork() defaults stdio to inherit (with the additional ipc) whereas spawn() defaults it to pipe.

from execa.

sindresorhus avatar sindresorhus commented on September 24, 2024

If anyone wants to take on this, see #108 for context and previous work.

@GMartigny @ehmicky I think you missed this. fork() is not really a good name, as fork is already a different thing in Unix: https://en.wikipedia.org/wiki/Fork_(system_call) The Node.js name is very unfortunate, since Node.js doesn't actually "fork" in the Unix sense.

from execa.

ehmicky avatar ehmicky commented on September 24, 2024

I agree that fork() is a bad name, because it does not map to Unix forks.

I think we should either:

  • try to mimic the Node.js behavior and keep the fork() name.
  • allow ourselves to deviate from the Node.js behavior and use a different name like execa.node() or execa.spawnNode().

Some things where we might consider deviating:
a) silent option defaulting to true instead of false
b) not offer any silent option at all? This option is not very useful, users can just use stdio-related options.
c) do not push ipc to stdio. It's very possible many users won't use that channel, so setting it up by default is not needed, and probably not cheap. The ones that do want it can specify it in stdio. Also half of the code in the current PR is related to this. However as @sindresorhus points it out in #108, if we don't do that, there might not be a need for this extra method at all, so we might still want to do it.

What do you think?

from execa.

sindresorhus avatar sindresorhus commented on September 24, 2024

allow ourselves to deviate from the Node.js behavior and use a different name like execa.node() or execa.spawnNode().

I think we should yes. Not sure which I like the most of execa.node() and execa.spawnNode() though.

b) not offer any silent option at all? This option is not very useful, users can just use stdio-related options.

I agree. We don't need it. It should just default to 'pipe' like normal.

c) do not push ipc to stdio. It's very possible many users won't use that channel, so setting it up by default is not needed, and probably not cheap.

I doubt it has any real performance impact.

from execa.

ehmicky avatar ehmicky commented on September 24, 2024

Alright, so the consequences would be:

  • rename fork() to node() or spawnNode()
  • do not add any silent option. The stdio options should default to pipe (like normal execa())

@GMartigny what do you think?

from execa.

GMartigny avatar GMartigny commented on September 24, 2024

Either .node or .spawnNode imply you can only use node as sub process. Should we get rid of the execPath option as well?

We can (and certainly should) keep execArgv to customize the sub process args.

from execa.

sindresorhus avatar sindresorhus commented on September 24, 2024

Should we get rid of the execPath option as well?

No, it's meant to be able to specify which Node.js executable you want. I already commented about clarifying the option description in your PR.

from execa.

issuehunt-oss avatar issuehunt-oss commented on September 24, 2024

@sindresorhus has rewarded $54.00 to @GMartigny. See it on IssueHunt

  • 💰 Total deposit: $60.00
  • 🎉 Repository reward(0%): $0.00
  • 🔧 Service fee(10%): $6.00

from execa.

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.