Rescheduling of node.js tasks

I am now writing a node.js socket application.

As it involves some CPU intensive tasks, it would be quite troublesome for multi-connections tasks.

Here is what the problem is: Let task A be the CPU intensive tasks, it is invoked by connection A. Let task B be the very lightweight tasks, it is invoked by all other connections, let say, > 10k connections.

When connection A invokes a lot of task A "nearly at the same time" (which is normal in my design, as every packet is needed to be processed). The tasks will be scheduled like this:

  • Task A
  • Task A
  • Task A
  • Task A
  • ....(maybe 10000 times of task A)
  • Task B

Task B cannot interrupt in the middle of task A tasks. So most likely, other users cannot do anything on this node.js server.

Except threading/cluster ,are there any ways to reschedule the tasks executed like this, make task A has the lowest priority?

  • Task A
  • Task A
  • Task B
  • Task A
  • Task A
  • Task A
  • Task A
  • Task B
  • Task A
  • ....

Thanks!

There is some possibilities:

  • When user A comes, can you just notice that you need to run A. Then you write a nodeJS file which is called by a cronjob and make all Task A.
  • You can make Task A async. Like this example: Fibonacci async