First post, libuv newbie.

I am trying to understand how a multi-threaded web server would fit in the 
libuv design. This is how I envision it, but I have some questions.

The main thread would run the HTTP accept loop, and I would expect each 
connection request to be handled to a worker in the thread pool. Each 
thread in the worker pool will also be doing I/O (think running a query 
against MySQL), and will somehow pass the results back to the main thread.

After reading a bit here and there, I would think this is one way to do 
this with libuv:

1. Main thread runs default loop and handles all the HTTP traffic. Whenever 
it gets an HTTP request, it puts it in a thread-safe queue and does a 
notify_all to awake one random thread in the pool; all of these threads 
would be waiting on the queue.

2. One worker thread awakes, dequeues pending work from the queue and runs 
it. I guess each thread has its own libuv loop, to handle the I/O needed to 
go to MySQL / read a file / etc.

3. Once the request has been processed, the worker thread notifies the main 
thread that the response is ready and tries to read the next pending 
request from the queue; iterate.

4. The main thread sends the response back to the HTTP client and closes 
that connection.

Now, there's some hand waving here, and I am not sure if what I'm 
describing is the proper / correct / best / possible way to do this with 
libuv. Some specific questions:

1. Is using a thread-safe queue a good match for libuv? If libuv provides 
something like this, I have not been able to find it, and that makes me 
doubt this would be the libuv way.

2. Is it possible / advisable to have multiple threads in a process, each 
handling I/O with its own libuv loop? I guess yes.

3. How would the worker threads notify the main thread that a response is 
ready? Would they use an async handle for this? If yes, can they all use 
the same async handle, or would each worker require their own async handle 
plugged to the main thread loop? But if they all share the same async 
handle, how does the main thread know which connection this response should 
be sent to?

Feel free to completely destroy this description and show me the right way 
to do this. Thanks for any hints and comments. Cheers!

You received this message because you are subscribed to the Google Groups 
"libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
To post to this group, send email to
Visit this group at
For more options, visit

Reply via email to