1

I am doing a shell execution on my event loop which is taking time and blocking my event loop. I am thinking of forking the shell command into child process, but my node process is running on a single core CPU. Is it possible to do forking?

majid
  • 29
  • 1
  • 2
  • Please show the shell execution code you're using in node.js. That should already be a different process. It shouldn't block the event loop unless you're using "sync" versions of child_process functions. Here's a hint. Questions about code here on stackoverflow will nearly always get faster and more accurate answers if you include your relevant code in the question. – jfriend00 Feb 19 '20 at 05:56
  • You can run multiple processes with a single core. The OS will time slice between them. Each process will get some CPU cycles. – jfriend00 Feb 19 '20 at 05:59
  • can you not wrap this in an async function / promise instead? I mean that is one of the big things about javascript right? Maybe this helps: https://stackoverflow.com/questions/50219748/how-to-use-javascript-async-await-on-executing-shell-script – The Fool Feb 19 '20 at 06:16
  • @TheFool - Wrapping something in an `async` function or in a promise doesn't make anything non-blocking. Promises are just a notification system. Code still does what the code always did, even if it's wrapped in a promise. An `async` function is no different. – jfriend00 Feb 19 '20 at 06:19
  • it doesnt make it non blocking but it doesnt block the main thread, that is the whole point as far as I know. – The Fool Feb 19 '20 at 06:21
  • @TheFool - Not true. Only truly asynchronous operations don't block the main thread. It doesn't make any different to the event loop whether they are wrapped in a promise or not. – jfriend00 Feb 19 '20 at 06:22
  • hmm that is very interesting. Do you have more material about this? What is true async and what not? Has it to do with context switching? – The Fool Feb 19 '20 at 06:23
  • @TheFool - True asynchronous operations are either implemented in a way that doesn't use CPU, but uses OS event notifications (like networking) or they are implemented using native code (with their own threads) in order to complete the operation and then insert events in the event queue when done (like the fs module for accessing files). Now, modern node.js has WorkerThreads so you can also run Javascript in its own thread now which is a different topic. This has gone a bit off-topic so I don't think we should continue filling up the comments here any more. – jfriend00 Feb 19 '20 at 06:34
  • @TheFool - [Overview of Blocking vs. Non-Blocking](https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/) and [The node.js event loop](https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/) and [libuv documentation](http://docs.libuv.org/en/v1.x/). – jfriend00 Feb 20 '20 at 04:45

0 Answers0