Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

My webserver has to make calls to 3-4 web services. I was going to implement it in Node.js, but was considering gevent since I'm not a huge fan of callback code. I understand that green threads are similar in behavior to OS threads, and each thread will wait for a response from one web service before calling the next. Is this correct?

For example, if I'm calling web services A, B, C, D, each of which take 1 second, node would have completed all 4 in 1 second (due to parallel calls), but gevent would take 4 seconds (since it chains them one after the other).

share|improve this question
add comment

2 Answers

The documentation for gevent specifically states that multiple jobs can run concurrently (http://www.gevent.org/intro.html#example). Since it uses LibUV under the hood and has a similar main event pump to nodejs, both approaches will be extremely similar but running in a different programming environment and syntax. Pick whatever flavor you prefer.

share|improve this answer
    
Thanks for your answer. I understand how different green threads can call web services concurrently (thread scheduling by the VM). Could you explain how a single green thread can execute multiple calls in parallel? –  tldr Jul 6 '13 at 21:41
    
I believe the term 'green thread' is inappropriate here; there is no emulation going on. The process relies on the operating system, I am most familiar with Windows so I will describe its simplified system, IOCP (Input/Output Completion Ports). The application will tell the OS to retrieve a resource and send a message when done. The main event pump will continue executing tasks your application provides, and once it notices an OS message it will add the completion to the event pump to be handled. So multiple resources are scheduled and the one thread can handle the completion events. :-) –  Roel van Uden Jul 6 '13 at 22:11
1  
gevent actually uses libev under the hood, not libuv. The libraries do pretty much the same things, just libuv has better support on Windows. –  Anorov Jul 8 '13 at 11:53
add comment

Basically, gevent subtasks will yield to each other when they encounter a blocking operation. I am no node expert, but I believe this is how node runs as well. A good example is here: http://sdiehl.github.io/gevent-tutorial/#synchronous-asynchronous-execution

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.