Reuse node async Task


Hello everybody,
I’m working with the Atom Task APIs in order to spawn a new node process and use it to run async code, so the UI is not blocked by long operations. Anyway, I realized it takes a relatively long time to create the task and put it in motion (maybe half-second, including some name resolutions). Plus, this is a pretty frequent scenario.

Is there a way I can create a separate node thread and keep it living on the background? I assume this would speed things up a bit.

Thank you guys!


The Electron team generally suggests opening a new invisible BrowserWindow to run background jobs like that.


Thank you a lot, I will give it a try.

In the meantime, I managed to create a task and reuse it as class variable. I use the constructor to instantiate it, and not the suggested once method.

# the childProcess inside myTask has always the same pid, so
# it's always the same process

if @myTask is null
  @myTask = new Task(taskPath)

@myTask.on 'custom-event', (status) ->
  # do something with the response

@myTask.start input, (response) ->
  # callback to call after the task is done

The task is implemented as follows:

myDep = require 'dependency'

module.exports = (input) ->

  return if input is null

  callback = @async()

  emit 'custom-event', 'started'

  # work with my stuff here, eventually will call the callback
  response = myDep.doSomething input
  callback response

It seems to work ok, but I’m experiencing several Unexpected end of input errors during the implementation, when I add apparently harmless lines of code. I’m getting through though. Moreover, one event does not seem to fire the callback properly.
Still investigating.