javascript - How to run child process in Mean Stack -
i have mean application uses nodejs, angularjs , expressjs.
here have called server angular controller below
angular controller.js
$http.post('/sample', $scope.sample).then(function (response) { -- -- } and in server.js below
app.post('/sample', usercontroller.postsample); here doing operation mongodb in post sample above code.
here got struck how calculation part have big calculation takes time (assume 1 hour) complete. client side trigger calculation angular controller.
my problem calculation should run in separately other uis , operations of other pages should not interupted.
i had seen child process in nodejs didn't understand how trigger or exec child process controller , if request in app.post possible access other pages.
edit:
i have planned in spawn child_process have problem continuing above.
lets consider application contains 3 users , 2 users accessing application @ same time. case if first person triggered child_process name first operation , in process , @ moment when second person need trigger process name 2nd operation needed calculate. here questions
- what happens if person started spawn command. if hangs or keep in queue or both execute parallel.
- if 2nd operation in queue when start operation.
- if 2nd operation in queue how can know how many in queue @ point of time
can solve.
note: question edited - see updates below.
you have few options it.
the straightforward way spawn child process express controller return response client once calculation done, if takes long may have problems socket timeouts etc. not block server or client (if don't use "sync" function on server , synchronous ajax on client) have problems connection hanging long.
another option use websocket or socket.io requests. client post message server wants computation started , server spawn child process, other things , when child returns send message client. disadvantage of new way of communication @ least there no problems timeouts.
to see how combine websocket or socket.io express, see answer has examples both websocket , socket.io - it's simple actually:
either way, spawn child process can use:
spawnexecexecfilefork
from core child_process module. make sure never use functions "sync" in name want because block server serving other requests entire time of waiting child finish - may hour in case, if second still ruin concurrency completely.
see docs:
update
some update edited question. consider example shell script:
#!/bin/sh sleep 5 date -is it waits 5 seconds , prints current time. consider example node app:
let child_process = require('child_process'); let app = require('express')(); app.get('/test', (req, res) => { child_process.execfile('./script.sh', (err, data) => { if (err) { return res.status(500).send('error'); } res.send(data); }); }); app.listen(3344, () => console.log('listening on 3344')); or using es2017 syntax:
let child_process = require('mz/child_process'); let app = require('express')(); app.get('/test', async (req, res) => { try { res.send((await child_process.execfile('./script.sh'))[0]); } catch (err) { res.status(500).send('error'); } }); app.listen(3344, () => console.log('listening on 3344')); it runs shell script requests on /test , returns result.
now start 2 requests @ same time:
curl localhost:3344/test & curl localhost:3344/test & curl localhost:3344/test & and see happens. if returned times differ 5 seconds , 1 response after 5 seconds intervals operations queued. if responses @ same time more or less same timestamp run in parallel.
sometimes it's best make experiment see happens.
Comments
Post a Comment