0

I have a restify api similar to this (i write in pseudocode) :

server.post('api/import') 
{

  database.write(status of this file.id is pending)
  fileModification(req.file)
  res.status(200)
  res.send(import has started)
} //here I do some file modifications and then i import it to database
server.get('api/import_info') 
{
  database.select(file status)
} //here I want to see status (is file imported or pending(process is not finished yet))

//In another module after import is finished I update database to 
database.write(file.id import status is completed)

Importing file is process that takes about 2 minutes, but even I don't wait for it to finish in api/import when I try to trigger 'info' route my api is blocked

Is it possible that event loop is blocked or maybe connection is not properly closed.

Thanks in advance

  • Show us the REAL code for the importing file operation that is blocking the event loop. If it's improperly using synchronous file I/O, then it should be converted to asynchronous file I/O so other requests can run while it's waiting on file I/O. If it's just doing lots of CPU-intensive stuff, then you will need to move it to another process or thread in any number of ways. – jfriend00 Oct 16 '19 at 12:24
  • You can see the various options for engaging other CPUs here: https://stackoverflow.com/questions/58384542/do-nodejs-async-functions-use-all-cpu-cores/58384746#58384746 or https://stackoverflow.com/questions/56917341/how-to-process-huge-array-of-objects-in-nodejs/56919401#56919401. – jfriend00 Oct 16 '19 at 12:28

1 Answers1

1

I have some ideas about your question.

  1. you can use cluster module Cluster, cluster module can create process depend on your cpu core. When on process blocked, Others process still can work.
  2. you can fork a new process in your api, use the new process handle your task.
MWY
  • 1,071
  • 7
  • 15