I am using GraphicsMagick for node. I basically crop the photos and retrieve the exif data of the photos uploaded by the user. I don't want to block the flow of request waiting for these task to be completed, therefore I need to use asynchronous functions to be able to do so. And I think I should be able to as these are I/O operations which node.js makes async itself.
But as I see all the functions in GraphicsMagick for node are synchronous functions. So I am not being able to sure as to how to achieve what I am looking for.
One idea that comes to my mind is to write a function with callback and have the GraphicsMagick processing done inside it. And use .nextTick() function to achieve asynchronous flow. But I am not totally sure if this is fine. And also are there any asynchronous functions for GraphicsMagick.
Please help me and an example code would be very appreciated as to how to get asynchronous functions from graphicsmagick.
The actual answer from @Saransh Mohapatra is actually wrong. As after little investigation turned out that all methods that perform operations over images, actually do not perform anything but only append arguments to the list that will be used after when you
write or call any buffer related methods executed in order to get/write actual image buffer.
Here is details over it in example of
- We call
- It calls
this.outwhich will call: https://github.com/aheckmann/gm/blob/master/lib/command.js#L49
- Which has method made for it when it was constructed: https://github.com/aheckmann/gm/blob/master/lib/command.js#L34
- Which all it does -
a.push(arguments[i]);and then concats it to all list (to other arguments).
- Thats it.
write is called:
- It gets list of arguments
- Which just filters off some reserved fields: https://github.com/aheckmann/gm/blob/master/lib/command.js#L274
- So then those arguments will be joined in
_spawnwhich is called from
- Thats it.
So based on this, any method that makes
operations over image, but do not save or persist buffer of it - do not need any async, as they actually do not do any work at all. So that means - you do need to worry about them.
The best approach for any heavy processing stuff is to use separate processes.
You can create another small node.js process, that will have some communication abilities with main process (ZeroMQ is good choice here).
This separate process have to be notified about file (path) and what to do with it, you can easily send that data from main process which makes such decisions via ZeroMQ.
This approach will allow you to have independence in the way main (web?) node processes work, as well as possibility in the future to scale to separate hardware/instances.
It is very good practice as well (unix-like application logic separation).