3 ms·
It isn't snake oil - it works and works well in the right situations. Sequential code is very readable, but not efficient at scale. If you run a single thread
by pcarmichael 17y ago
It isn't snake oil - it works and works well in the right situations.
Sequential code is very readable, but not efficient at scale. If you run a single thread (without the event based approach), each request must wait for preceding requests to finish entirely before running. That can be extremely bad for latency. On the other end, say you run a thread per socket - that's not effective as the memory usage for reserving thread stack space will limit your maximum connection count.
Event programming is useful in that it effectively performs cooperative multitasking without the overhead of thread stack space. Multiple requests can be running in parallel without fully blocking each other. Within a single thread they do serialize, but only by the length of your callbacks. If a request has to do I/O, it will queue up the operation and then yield so that other callbacks can run. Ultimately, it is reasonably fair scheduling-wise, eliminates time wasted on blocking I/O, and is more memory efficient than threading when handling large numbers of simultaneous requests/connections.