Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,288 members, 7,807,977 topics. Date: Thursday, 25 April 2024 at 12:56 AM

Child Threads Handled In Node.js - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Child Threads Handled In Node.js (784 Views)

I Have This Annoying Bug In Node Js For The Past 5 Days..help / Building An API In Node.js And Taking It Live On A VPS / Free PHP, Node js ,Mongo db, Angular And C# Proje Source Code With Documentation (2) (3) (4)

(1) (Reply) (Go Down)

Child Threads Handled In Node.js by Arjunkumar09: 9:59am On Jan 29, 2020
Hello Everyone, I want to know how are ‘Child Threads’ handled in Node.js? In my previous interview, I have faced this question but I didn’t know the answer. Can anyone knows these query and help me out or suggest some node.js based interview questions as a beginner point.
Re: Child Threads Handled In Node.js by peterincredible: 8:14pm On Jan 29, 2020
Arjunkumar09:
Hello Everyone, I want to know how are ‘Child Threads’ handled in Node.js? In my previous interview, I have faced this question but I didn’t know the answer. Can anyone knows these query and help me out or suggest some node.js based interview questions as a beginner point.
pls i dont get your question child thread asin how pls put code so we can understand or explain further with some code am willing to help but i just dont get ure question
Re: Child Threads Handled In Node.js by makavele: 8:39pm On Jan 29, 2020
peterincredible:
pls i dont get your question child thread asin how pls put code so we can understand or explain further with some code am willing to help but i just dont get ure question

^ The question albeit a very advanced part of javascript, is pretty clear and straightforward.

Nodejs doesn't allow the users interfere and mess with the running of child threads. By default, it is a single-thread non-blocking runtime, as we all know.
However, if there is a need to perform any asynchronous I/O task, then Node.js can spawn child-threads.
You can invoke this though, using the 'child-process' module that comes with Node.

I did something like this a while back:

const oldSpawn = childProcess.exec;
childProcess.exec = () => {
Logger.info('spawn called');
Logger.info(arguments);
return oldSpawn.apply(this, arguments);
};


It relies heavily on the child_process.spawn() command and take note, this doesn't block the event loop in any way.
If you wanted to run shell commands in between Node js operations, then you can spawn a child process and use the exec command. I see people do this a lot:

import { execSync } from 'child_process';
execSync .. blah blah blah

Take note, that execSync will block the event loop. Although, it is very good for automating shell scripts.

You could also fork a child, (child_process.fork()), which will create a communication channel between them both i.e parent and child.

=======

I hope I have been of service to you. Cheers.

p.s.: contact me and I can schedule a mock interview for you.
Re: Child Threads Handled In Node.js by Nobody: 10:01am On Jan 30, 2020
makavele:


^ The question albeit a very advanced part of javascript, is pretty clear and straightforward.

Nodejs doesn't allow the users interfere and mess with the running of child threads. By default, it is a single-thread non-blocking runtime, as we all know.
However, if there is a need to perform any asynchronous I/O task, then Node.js can spawn child-threads.
You can invoke this though, using the 'child-process' module that comes with Node.

I did something like this a while back:




It relies heavily on the child_process.spawn() command and take note, this doesn't block the event loop in any way.
If you wanted to run shell commands in between Node js operations, then you can spawn a child process and use the exec command. I see people do this a lot:



Take note, that execSync will block the event loop. Although, it is very good for automating shell scripts.

You could also fork a child, (child_process.fork()), which will create a communication channel between them both i.e parent and child.

=======

I hope I have been of service to you. Cheers.

p.s.: contact me and I can schedule a mock interview for you.
I will love to take you up on this offer in a couple of months
Re: Child Threads Handled In Node.js by makavele: 3:20pm On Jan 30, 2020
crownedrookie:

I will love to take you up on this offer in a couple of months

You're welcome in advance

1 Like

Re: Child Threads Handled In Node.js by Nobody: 6:20pm On Jan 30, 2020
makavele:


You're welcome in advance
What kind of work are you into currently if I may ask bro
Re: Child Threads Handled In Node.js by makavele: 6:49pm On Jan 30, 2020
crownedrookie:

What kind of work are you into currently if I may ask bro

Software Engineering :: Technical Lead
Re: Child Threads Handled In Node.js by peterincredible: 10:16pm On Jan 30, 2020
makavele:


Software Engineering :: Technical Lead
i sent a message to u on watsapp
Re: Child Threads Handled In Node.js by makavele: 10:32pm On Jan 30, 2020
^ Whatsapp ke??

I sent you a PM . . check your Nairaland associated email . .
Re: Child Threads Handled In Node.js by rohanjoshi0894: 9:12am On Jan 31, 2020
Arjunkumar09:
Hello Everyone, I want to know how are ‘Child Threads’ handled in Node.js? In my previous interview, I have faced this question but I didn’t know the answer. Can anyone knows these query and help me out or suggest some node.js based interview questions as a beginner point.

Node.js, in its essence, is a single thread process. It does not expose child threads and thread management methods to the developer. Technically, Node.js does spawn child threads for certain tasks such as asynchronous I/O, but these run behind the scenes and do not execute any application JavaScript code, nor block the main event loop.

If threading support is desired in a Node.js application, there are tools available to enable it, such as the ChildProcess module.

Also, here we have listed node.js interview questions:

Question: Explain the basics of Node.js.
Question: How are ‘Child Threads’ handled in Node.js?
Question: State some differences between Angular JS and Node.js.
Question: State the primary uses of Node.js.
Question: What is Event-Driven programming?
Question: In the context of Node.js, what is REPL?
Question: What is a test pyramid in Node.js?
Question: What is libuv?
Question: Is Node.js the best platform for CPU-heavy applications?
Question: What is the purpose of the Express JS Package?
Question: What are LTS versions of Node.js?
Question: Explain the working of assert in Node.js
Question: What is callback hell?
Question: What is stub in Node.js?
Question: What is Event Loop?
Question: What is stream in Node.js? What are its types?
Question: List and explain the timing features of Node.js.
Question: Highlight the differences between process.nextTick() and setImmediate().
Question: Explain readFile and createReadStream in Node.js.
Question: Does Node.js provide a Debugger?
Question: Describe the exit codes in Node.js.
Question: Why is NODE_ENV used?
Question: What is Event Emitter in Node.js?
Question: What is Punycode?
Question: Explain the concept of JIT and highlight its relation with Node.js.
Question: Why is the buffer class used in Node.js?
Question: What is the difference between fork () and spawn () methods in Node.js?
Question: State the steps to write an Express JS application.
To get the answer to these questions you can [url="https://hackr.io/blog/node-js-interview-questions"]follow us[/url].
Re: Child Threads Handled In Node.js by rubygem: 9:19am On Jan 31, 2020
The os and cluster core module can also handle child processes.However, unlike the core child-process module, the child process can't communicate with the parent process.
Here is how it is implemented:

var cluster = require('cluster');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {

console.log('I am the master/parent process launching workers!');

for(var i = 0; i < numCPUs; i++) {cluster.fork();} }
else {
console.log('I am a fork/child process'); //perform child operations }
Re: Child Threads Handled In Node.js by makavele: 10:53am On Jan 31, 2020
@rohanjoshi0894

Stop pasting google answers.
Show that you really know nothing about the subject in question.


https://www.google.com/search?q=Node.js%2C+in+its+essence%2C+is+a+single+thread+process.+It+does+not+expose+child+threads+and+thread+management+methods+to+the+developer.+Technically%2C+Node.js+does+spawn+child+threads+for+certain+tasks+such+as+asynchronous+I%2FO%2C+but+these+run+behind+the+scenes+and+do+not+execute+any+application+JavaScript+code%2C+nor+block+the+main+event+loop.&oq=Node.js%2C+in+its+essence%2C+is+a+single+thread+process.+It+does+not+expose+child+threads+and+thread+management+methods+to+the+developer.+Technically%2C+Node.js+does+spawn+child+threads+for+certain+tasks+such+as+asynchronous+I%2FO%2C+but+these+run+behind+the+scenes+and+do+not+execute+any+application+JavaScript+code%2C+nor+block+the+main+event+loop.&aqs=chrome..69i57&sourceid=chrome&ie=UTF-8

(1) (Reply)

Visual Game Programmer Needed / Why Do I Keep Failing ? / How To Master C# And .net Framework - Beginner’s Roadmap

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 26
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.