Is it beneficial to create an asynchronous function?

78 views
Skip to first unread message

Robert Steckroth

unread,
Jun 16, 2013, 4:27:19 PM6/16/13
to nodejs
I have a server which parses through a text file and creates a XML page.
The XML creation can involve a lot of loops (up to ~8000).
I am confused regarding the asynchronous nature of NodeJs.
Would the below function benefit from a non-blocking call? Or does the createServer function maintain its own thread for each incoming request?

You can see this function call on line: 65 of the provided Node file.
makeXmlFromString(textOutput, function(xmlText)

http://bazaar.launchpad.net/~robertsteckroth/stock-ticker-mobile-app/proxy/view/head:/server/stock_proxy_server.js

Or maybe this file is done correctly... I am having a lot of trouble getting the scoop on async in JavaScript :0
-- 
Bust0ut, Surgemcgee: Systems/Web/Software Engineer

"Freedom is empowered by those without power" - Robert Edward Steckroth II
"Injustice, regarding the internet, is fashioned through laws and enforcement" - Robert Edward Steckroth II
"Reality is the fuel of the universe and the roadblock to human understanding" - Robert Edward Steckroth II


Angel Java Lopez

unread,
Jun 16, 2013, 4:37:37 PM6/16/13
to nod...@googlegroups.com
Hi!

Ummm... makeXmlFromString, beginning at line 83, could directly return the value xmlFile instead of calling a callback. No I/O in its body, so you don't leverage the power of node.js to manage async I/O

Maybe, at line 61, when req.on('data'....) you could start to create the xml, incrementally. But if it is hard, that's ok, for a first approach

Any comments?

Angel "Java" Lopez
@ajlopez



--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Robert Steckroth

unread,
Jun 16, 2013, 6:26:46 PM6/16/13
to nodejs
So, how do I get the function to be asynchronous if I added other functionality? Is there a module to import? I just do not get the threading aspect of it all I guess.

Mark Hahn

unread,
Jun 16, 2013, 7:37:41 PM6/16/13
to nod...@googlegroups.com
So, how do I get the function to be asynchronous

You can't.  You could put in setTimeouts with zero delay if you are worried about giving other tasks a chance of running more often.

I just do not get the threading aspect of it all 

It is actually simple, there is not threading.  You can launch other processes if you wish.

What evidence do you have that the running time of your code is a problem for your application?  How many milliseconds does it consume?  Are you prematurely optimizing?

Andrey

unread,
Jun 17, 2013, 12:45:14 AM6/17/13
to nod...@googlegroups.com
Do you have any numbers to proof that you need to optimise here? How much time does it usually take for makeXmlFromString to complete? Could you post first lines of processed v8.log after running with --profile?

If you are sure that this function does block io loop for some significant amount of time (say, 10ms - how small this number should be really depend on what you expect from your application) then you have following options:
 - write C++ addon, use libuv work queue ( http://nikhilm.github.io/uvbook/threads.html#libuv-work-queue ). Measure again, note that it could be actually slower than JS only ( see http://kkaefer.github.io/node-cpp-modules/#benchmark-thread-pool )
 - as already suggested, you can try allow IO loop to run in parallel with your code by splitting in smaller functions wrapped in setTimeout
 - move your code to separate node.js process, use some kind of RPC to send arguments/receive results (for example, fork/message -  http://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options )

Robert Steckroth

unread,
Jun 17, 2013, 7:59:01 PM6/17/13
to nodejs
Wow, you have many solutions to that one. I was under the impression that a consistent asynchronous patterned was expected in NodeJs programming. I think I understand how it works now. It just serves users with data when it is time/ready to. I was thinking the wrong way about the whole thing. I did find your solutions interesting however, and I thank you for them (except setTimeout, jessh;).

Also, I posted the processed v8.log file here --> http://dpaste.com/hold/1248901/
It does not seam like all of those 0%'s are a good thing but I still need to go over some docs to grasp what it means.

Andrey Sidorov

unread,
Jun 17, 2013, 8:26:25 PM6/17/13
to nod...@googlegroups.com
"consistent asynchronous pattern" is expected for IO-bound tasks.
CPU-bound solutions are less consistent as 1) it's a little bit
outside of node.js scope 2) there are more solutions 3) it depend a
lot on domain area (not all people crunch fibonacci numbers) and
hardware ( number of CPUs/GPU/vector processing/special hardware/etc)

From your v8.log it looks like you don't need to optimise anything

Andrey
> You received this message because you are subscribed to a topic in the
> Google Groups "nodejs" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/nodejs/qzOkCtimKn4/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
Reply all
Reply to author
Forward
0 new messages