Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending objects as parameters to forked nodejs processes

Tags:

node.js

Having fork('./MyProcess.js',['foo']); in the main process and console.log(process.argv[2]); in the forked process will log foo to my console.

However, fork('./MyProcess.js',[{myProp : 'bar'}]); in the main process and console.log(process.argv[2]); console.log(process.argv[2].myProp); in the forked process will log [object Object] (as expected) but undefined for the second log.

Why is this, and what should I do to get the desired behavior?

like image 317
Jonah Avatar asked Dec 18 '22 16:12

Jonah


1 Answers

Pass object(json) to child process through commadline arguments is not a good idea, command arguments need escape first(not easy). You have some choice:

  1. hex encode the json then pass to child process, this works but your child process interface is bad.
  2. save json to a file, pass the file path instead.
  3. pass the json to child process through stdin.
  4. send the json as message to child process, see https://nodejs.org/api/child_process.html#child_process_child_send_message_sendhandle_options_callback
like image 54
tangxinfa Avatar answered Jan 13 '23 05:01

tangxinfa